我是新來的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'成功。如果失敗了,這將解釋爲什麼你看到未初始化的垃圾。 –