2013-11-09 72 views
1

因此,我将字符串转换为C++中的字节,但是当它将它添加到注册表中时,它将剥离exe部分但保留了。,我不知道它有什么问题。C++字符串到字节错误

如果你想知道NXS是什么,它的价值是“noerrorsplease.exe”,类型是char。

char szFinal[] = ""; 
strcat(szFinal, (const char *)ExtractDirectory(filepath).c_str()); 
//Not needed: strcat(szFinal, ""); 
strcat(szFinal, nxs); 
strcat(szFinal, ".exe"); 

     CString str; 
     str = szFinal; 
     str += ".exe"; 
     cout << str.GetString() << endl; 
     const BYTE* pb = reinterpret_cast<const BYTE*>(str.GetString()); 
     cout << pb << endl; 
     DWORD pathLenInBytes = *szFinal * sizeof(*szFinal); 
     if(RegSetValueEx(newValue, TEXT("Printing Device"), 0, REG_SZ, (LPBYTE)pb, pathLenInBytes) != ERROR_SUCCESS) 
     { 
      RegCloseKey(newValue); 
      cout << "error" << endl; 
     } 
     cout << "Possibly worked." << endl; 
     RegCloseKey(newValue); 
+0

哦,我修正了我自己的错误,呵呵。它正盯着我的脸!大声笑 – user2948772

+0

不得不将代码替换为dword pathleninbytes:DWORD pathLenInBytes = * str * sizeof(* str); – user2948772

回答

2

此代码

char szFinal[] = ""; 
strcat(szFinal, (const char *)ExtractDirectory(filepath).c_str()); 

已经是无效的。您定义的数组szFina只有一个字符是终止零。你不能用它来拷贝任何字符串。在这些情况下,您应该使用std :: string类型的对象。

+0

+1有* *可以连接的字符串,但它很枯燥('“”')。无论如何,这仍然是一个严重的问题,应该加以解决。 – WhozCraig

+0

是的,我发布后差不多10分钟就发现自己的错误。 – user2948772