function in dll doesn't receive CString argument value

♀尐吖头ヾ 提交于 2019-12-12 02:34:48

问题


Hi everyone. I have to work with old utility: which converts xls into txt. There was a small problem in logic of the utility, but the problem is in other thing... The utility consists of two parts: exe module and dll module, and uses MFC.

In exe project we have

pInit = (t_bXR_Init)GetProcAddress(hExcel, _T("bXR_Init"));

and

pInit("logfiles",false);

In dll project we have

typedef bool (*t_bXR_Init) (CString const &strlogfilespath, bool btxtfile); XLSREADER_API bool bXR_Init(CString const &strlogfilespath, bool btxtfile);

The problem is when we send argument "logfiles" into the function it doesn't get it. It's strange, 'cause all other parameters are send properly.

The reason is somehow connected with using of CString. But I don't know how...

XLSREADER_API is defined as:

#define XLSREADER_API extern "C" __declspec(dllimport)

Also I've added

AFX_MANAGE_STATE(AfxGetStaticModuleState());

in the beginning of function's body (for bXR_Init). But it didn't help.

Also I tried to change some settings for these two projects, all settings are the same (e.g. calling conversion is __cldecl(/Gd); I build either debug versions exe and dll or release version of exe and dll simultaneously).

Also I tried to use CString instead of CString& - the same situation. It works properly if use char*, but boss says to find what the origin of the problem is at first.

What may lead to the problem (the function doesn't get CString parameter)?


回答1:


To pass a complex type such as a CString across a DLL boundary you have to make sure that both the DLL and the exe are using the exact same DLL libraries. Set "Runtime Library" to multi-threaded DLL and set "Use of MFC" to Use MFC in a Shared DLL. Also, don't mix debug and release modules: Both must be the same.

Without these conditions you get two different heaps, and you can't keep the allocations/deletions compatible with two heaps.




回答2:


Try passing an actual CString parameter to the call:

CString sPath = "logfiles";
pInit(sPath,false);



回答3:


wtfigo! (what the f is going on)

the problem is solved.

I discovered, that exe project had "character set" = "use multibyte character set" and dll project had "character set" = "use unicode character set".

So, dll function got CString with char* inside, but considered it as CString with wchat_t* inside. And it looked as garbage (as complete garbage on my pc and as chinese symbols on my workmate's pc).

I changed "character set" for exe project to "use unicode character set" and discovered about 60 errors. Then I read an article http://habrahabr.ru/post/164193/ (in russian; or in english: http://www.codeproject.com/Articles/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc).

And fixed all errors, widely used macroses from TCHAR.h (MSDN helped me).

Thanks everybody for your help.



来源:https://stackoverflow.com/questions/17947942/function-in-dll-doesnt-receive-cstring-argument-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!