【发布时间】:2012-07-28 18:36:31
【问题描述】:
我之前问过一个关于 delphi 和 C/C++ DLL 的问题。
我现在有另一个关于记录/结构的问题。 DLL 应该能够从 MainAPP 动态更改指针变量的值。
我的delphi MAINAPP有如下记录:
type MyRec = record
MyInteger : Pointer;
MyWideString : pwidechar;
MyString : pchar;
MyBool : Pointer
end;
type
TMyFunc = function ( p : pointer ): pointer; stdcall;
procedure test;
var
MyFunction : TMyFunc;
TheRecord : MyRec;
AnInteger : Integer;
AWideString : WideString;
AString : String;
ABool : Bool;
begin
AnInteger := 1234;
AWideString := 'hello';
AString := 'hello2';
ABool := TRUE;
TheRecord.MyInteger := @AnInteger;
TheRecord.MyWideString := pwidechar(AWideString);
TheRecord.AString := pchar(AString);
TheRecord.ABool := @ABool;
[...]
@MyFunction := GetProcAddress...
[...]
MyFunction (@TheRecord); // now the DLL should be able to change the values dynamically.
MessageBoxW (0, pwidechar(AWideString), '', 0); // Show the results how the DLL changed the String to...
end;
C/C++ 代码(只是示例)
typedef struct _TestStruct{
void *TheInteger; // Pointer to Integer
wchar_t *TheWideString; // Pointer to WideString
char *TheAnsiString; // Pointer to AnsiString
bool *TheBool // Pointer to Bool
}TestStruct;
__declspec(dllexport) PVOID __stdcall MyExportedFunc (TestStruct *PTestStruct)
{
MessageBoxW(0 ,PTestStruct->TheWideString, L"Debug" , 0); // We read the value.
PTestStruct->TheWideString = L"Let me change the value here.";
return 0;
}
由于某些原因,它会崩溃等。 我做错了什么?
感谢您的帮助。
【问题讨论】:
-
你在调用之前检查了 MyFunction 引用 nil 吗?
-
Delphi 中的 MyRec 记录声明与 _TestStruct C 结构中的字段顺序不同
-
@BenjaminWeiss:删除了我之前的答案 - 你说 C++ 函数调用成功,然后在调用后某处失败。你能指出失败的代码行吗?
-
对潜在的回答者保持礼貌 - 包括您在应用崩溃时收到的错误消息。
-
向您的朋友询问她/他正在收到的错误消息/异常类型
标签: c++ c delphi memory-management dll