C++ 一些奇怪的腐败问题
下面是我为通过串行端口输出可视数据而修改的winamp插件的一些片段C++ 一些奇怪的腐败问题,c++,corruption,C++,Corruption,下面是我为通过串行端口输出可视数据而修改的winamp插件的一些片段 char * overrideCom = NULL; char * cPortName; void config(struct winampVisModule *this_mod) { MessageBox(this_mod->hwndParent, cPortName, "Serial Port", MB_OK); // Tell us what the value is } // This functio
char * overrideCom = NULL;
char * cPortName;
void config(struct winampVisModule *this_mod)
{
MessageBox(this_mod->hwndParent, cPortName, "Serial Port", MB_OK); // Tell us what the value is
}
// This function will convert a System^ string into a std::string
std::string makeStd(String ^in){
array<Byte, 1> ^chars = System::Text::Encoding::ASCII->GetBytes(in);
pin_ptr<Byte> charsPointer = &(chars[0]);
char *nativeCharsPointer = reinterpret_cast<char *>(static_cast<unsigned char *>(charsPointer));
std::string native(nativeCharsPointer, chars->Length);
return nativeCharsPointer;
}
// This function will grab the second indexed com port and return it in an easily convertable std::string instead of String^
std::string getComPort(){
array<String^, 1> ^serialPorts = nullptr;
LPTSTR out;
try {
serialPorts = SerialPort::GetPortNames();
}
catch (Win32Exception^ ex)
{
MessageBox(NULL, "Failed to find COM port!","Initialize Failed!", MB_OK);
}
std::string portb = makeStd(serialPorts[1]);
out = const_cast<char *>(portb.c_str());
// Make sure I'm not crazy:
MessageBox(NULL, out, "Com port!", MB_OK);
return portb;
}
int init(struct winampVisModule *this_mod)
{
config_read(this_mod);
//std::string port = getComPort();
//cPortName = const_cast<char *>(port.c_str());
// This would normally work but for some reason causes corruption of the string and doesn't connect properly:
if (overrideCom == NULL || overrideCom[0] == 0){
std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());
}
else
{
cPortName = overrideCom;
}
return 0;
}
void config_getinifn(struct winampVisModule *this_mod, char *ini_file)
{ // makes a .ini file in the winamp directory named "plugin.ini"
char *p;
GetModuleFileName(this_mod->hDllInstance,ini_file,MAX_PATH);
p=ini_file+strlen(ini_file);
while (p >= ini_file && *p != '\\') p--;
if (++p >= ini_file) *p = 0;
strcat(ini_file,"plugin.ini");
}
void config_read(struct winampVisModule *this_mod)
{
char ini_file[MAX_PATH];
char* tResult = new char[255];
config_getinifn(this_mod,ini_file);
config_x = GetPrivateProfileInt(this_mod->description,"Screen_x",config_x,ini_file);
config_y = GetPrivateProfileInt(this_mod->description,"Screen_y",config_y,ini_file);
// Grab the overrideCom= param.
GetPrivateProfileString(this_mod->description, "overrideCom", NULL, tResult, 255, ini_file);
overrideCom = tResult;
}
当我省略上面的if语句时,config()输出:
我不明白
而且,
当在INI文件中指定overrideCom参数时,它可以正常工作
我的目标是不依赖INI文件,允许它自动检测第二个com端口,但允许在需要时覆盖INI
DOH!解决方案是将portx声明为全局变量。
std::string portx=getComPort();
std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());
cPortName=const_cast(portx.c_str());
在下一行,
portx
会被销毁,它的数据也会被销毁。因此,cPortName
只是一个不指向任何东西的悬空指针。Microsoft已经为您的MakeStd
函数提供了一个更好的版本--marshall\u as
。例如,通过包含正确的头文件,您可以执行marshal\u as(dotnetString)
if (overrideCom == NULL || overrideCom[0] == 0){
std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());
}
else
{
cPortName = overrideCom;
}
Èêtw
COM3
std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());