我是新来的c + +,我在一个检查扫描器的项目,我使用的扫描仪提供的API。这里是我的代码:数组指针的向量C++
.h文件中:
#include <iostream>
#include<Windows.h>
#include<vector>
using namespace std;
class Excella
{
public:
vector<char*> getDevicesName();
};
.cpp文件:
vector<char*> Excella::getDevicesName()
{
DWORD dwResult;
vector<char*> listeDevices;
char pcDevName[128]="";
int i = 6;
// the device's name is stored in the variable 'pcDevName'
while ((dwResult = MTMICRGetDevice(i, (char*)pcDevName)) != MICR_ST_DEVICE_NOT_FOUND) {
dwResult = MTMICRGetDevice(i, (char*)pcDevName);
i++;
listeDevices.push_back((char*) pcDevName);
}
return listeDevices;
}
的main.cpp
vector<char*> liste = excella.getDevicesName();
if (liste.empty()!= true)
{
for (vector<char*>::iterator IterateurListe = liste.begin(); IterateurListe != liste.end(); ++IterateurListe)
{ string str(*IterateurListe);
auto managed = gcnew String(str.c_str());
devices->Items->Add(managed);
}
}
else {
MessageBox::Show("The vector is empty");
}
的问题是,我可以得到正确的设备号码..我只是有一些怪异的字符。
谢谢你的帮助。
你能否提供一些细节?你会得到什么样的怪异人物? – swinefish
对于一个'pcDevName'已经是'char *'所以摆脱不需要的'char *'强制转换。 –
对于两种情况,在将'pcDevName'推入向量之前,您从不检查'dwResult'的结果以确保''MTMICRGetDevice'成功。如果失败了,这将解释为什么你看到未初始化的垃圾。 –