当被调用的DLL向调用方DLL返回值时,返回值损坏 [英] return value corrupted when the called DLL return value to the caller DLL

查看:68
本文介绍了当被调用的DLL向调用方DLL返回值时,返回值损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个DLL DLL1和DLL2
DLL1调用DLL2函数

Hi,

I have two DLLs DLL1 and DLL2
DLL1 calls DLL2 function

char* Execute(char*,char*,...)      [DLL2 function]

char * retVal = obj->Execute("Test","","");




在DLL2函数中参数正确传递并在DLL2中正确评估.
但是当DLL2 Execute函数将值返回给调用方函数时,例如:




In DLL2 function Parameters passed correctly and evaluated in DLL2 correctly.
but when DLL2 Execute function return value to caller function like :

char* retVal = obj->Execute("Test","","");


retVal正在获取垃圾值.

我已经尝试了以下替代方法:

字符串retVal以及更改函数的返回类型 string& Execute(char *,char *,...)

每次我得到垃圾值.
DLL1在发布模式下构建,而DLL2在调试模式下构建.[无法更改模式].

让我知道是否有人帮助.


retVal is getting garbage values.

I have already tried with following alternatives:

string retVal and also changing function return type string& Execute(char*,char*,...)

Every time I got garbage value.
DLL1 is built in Release mode and DLL2 in debug mode.[can not change the mode].

Let me know if any one knows any help.

推荐答案



似乎您正在返回存储在堆栈中的字符串.

您可以使用new/malloc分配字符串并返回指针,但必须记住要在创建它的dll中释放该指针.

除非您将FreeString api添加到您的dll中,否则在这种特殊情况下,我建议使用SysAllocString或GlobalAlloc之类的Windows api.

问候,
Mauro H. Leggieri
Hi,

It seems you are returning a string stored in the stack.

You can allocate the string with new/malloc and return the pointer but you have to remember to free that pointer inside the dll that created it.

Unless you add a FreeString api to your dll, I recommend to use Windows api''s like SysAllocString or GlobalAlloc for this special cases.

Regards,
Mauro H. Leggieri


您可以做的另一件事是像这样传递对Execute函数的引用:

Another thing you could do is to pass a reference to the Execute function like this:

void Execute(char **result, int resultLength, char *, char *, ...);



您需要在调用之前为结果分配内存,然后在DLL1中完成操作后自己释放它.

如果在调用Execute之前不知道结果的长度,则可以对Microsoft使用它们的库执行相同的操作,并在第一次传入NULL时执行. Execute将为您填充resultLength,然后您可以相应地分配内存.

我自己做了很多.



You would need to allocate memory for the result before the call and then free it yourself when done from within DLL1.

If you don''t know the length of the result before the call to Execute, you could do the same thing Microsoft does with their libraries, and pass in NULL to Execute the first time. Execute would fill in the resultLength for you and then you could allocate your memory accordingly.

I do this a lot myself.


这篇关于当被调用的DLL向调用方DLL返回值时,返回值损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆