【发布时间】:2017-08-21 12:08:36
【问题描述】:
我有一个用 /mt 编译的 dll。该 dll 导出一个函数“main”,我使用 rundll32 从 cmd 调用该函数。此函数将从我包含的头文件中调用另一个函数,该头文件应该采用存储在注册表中 Environment 下的一些设置。想象一下我在做什么,这里有一些代码:
Dll:main
#include "..\Header.h"
extern "C" void APIEXP WINAPI main(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow){
char *parentPath = nullptr;
size_t sz;
DWORD buffSize;
block; //this is a messagebox
buffSize = sizeof(client.pc_name);
if (!GetComputerNameA(client.pc_name, &buffSize)) strcpy(client.pc_name, "");
if (!GetUserNameA(client.user_name, &buffSize)) strcpy(client.user_name, "");
client.os_time = time(0);
client.version = Version;
client.os_version = Exe_version;
TextSetting("pc_description", client.pc_description);
TextSetting("custom_name", client.custom_name);
}
标题
LPSTR TextSetting(LPSTR setting,LPSTR buff) {
//Returns all data in buff
DWORD type, sz;
msg("dll: ","function called");
if (RegGetValueA(HKEY_CURRENT_USER,"Environment", setting, RRF_RT_REG_SZ | RRF_RT_REG_MULTI_SZ, &type, (LPBYTE)buff, &sz) != ERROR_SUCCESS) {
buff[0] = 0;
msg("sdsv: ", "err");
return buff;
}
return buff;
}
LPSTR con(LPSTR dest, LPSTR a, LPSTR b, LPSTR c) {
//c is optional and default: ""
strcpy(dest, a);
strcat(dest, b);
strcat(dest, c);
return dest;
}
VOID msg(LPSTR app,LPSTR a) {
char temp_buffer[2048];
con(temp_buffer , app,a, "\n");
OutputDebugStringA(temp_buffer);
}
上面显示的代码完美运行:这里的重点是 main 中的 TextSetting。它按应有的方式检索数据。但是当我从 TextSetting 中删除 msg("dll: ","function called"); 时,它只会给出一个错误(返回码不是 ERROR_SUCCESS)。此外,我什至无法调试该函数。当我将调试器附加到 rundll32 进程时,我在 TextSettings 中的 if 语句上设置的断点给出了标题中提到的警告。
我注意到一些额外的事情: 如果我调用 msg 或 con 函数,它就可以工作(看起来 con 中使用的 str* 函数使代码工作)。更进一步:如果我只在 if 语句之前使用 strcpy 和 strcat ,我也可以工作。我不知道问题是什么(也许是一些缓冲区溢出奇迹般地修复了 if 语句中使用的代码?)
【问题讨论】:
-
这是一个白痴问题:你是在Debug配置下编译的吗?
-
最初发布但随后作为调试..没有变化。很奇怪,因为我知道在 Release 下调试器应该可以工作。但确实如此。该项目很大,我之前从未将其更改为调试(仅几次)。我认为它不会以任何方式影响行为。屏幕截图是在发布时拍摄的..
-
如果您关闭优化并生成调试符号,则可以在发布配置中进行调试。调试配置的重点是让它更容易做到。
标签: c++ debugging dll c-strings