一些奇怪的腐败问题
some odd corruption issues
这是我一直在修改的用于通过串行端口输出可视数据的 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 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;
}
当我 运行 没有 overrideCom 参数的代码时,它应该通过获取它能找到的第二个索引 com 端口来补偿,单独它工作正常:
std::string port = getComPort();
cPortName = const_cast<char *>(port.c_str());
但这不起作用:
if (overrideCom == NULL || overrideCom[0] == 0){
std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());
}
else
{
cPortName = overrideCom;
}
端口未连接,当我调用 config() 时,我收到一个消息框:
Èêtw
当我省略上面的 if 语句时,config() 输出:
COM3
我不明白!
此外,
当在 INI 文件中指定 overrideCom 参数时,它工作得很好。
我的目标是不依赖 INI 文件并允许它自动检测第二个 com 端口,但如果需要,允许在 INI 中进行覆盖。
DOH!解决方案是将 portx 声明为全局变量。
std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());
在下一行 portx
被销毁,它的数据也是如此。所以 cPortName
只是一个指向任何东西的悬空指针。
这是我一直在修改的用于通过串行端口输出可视数据的 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 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;
}
当我 运行 没有 overrideCom 参数的代码时,它应该通过获取它能找到的第二个索引 com 端口来补偿,单独它工作正常:
std::string port = getComPort();
cPortName = const_cast<char *>(port.c_str());
但这不起作用:
if (overrideCom == NULL || overrideCom[0] == 0){
std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());
}
else
{
cPortName = overrideCom;
}
端口未连接,当我调用 config() 时,我收到一个消息框:
Èêtw
当我省略上面的 if 语句时,config() 输出:
COM3
我不明白!
此外, 当在 INI 文件中指定 overrideCom 参数时,它工作得很好。
我的目标是不依赖 INI 文件并允许它自动检测第二个 com 端口,但如果需要,允许在 INI 中进行覆盖。
DOH!解决方案是将 portx 声明为全局变量。
std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());
在下一行 portx
被销毁,它的数据也是如此。所以 cPortName
只是一个指向任何东西的悬空指针。