Hello guys.
I have a DLL containing this function:
function DCL_Initialize(bDeviceID: Byte; sIP: PAnsiChar): Byte; stdcall external 'DCL.dll';
now I want to call this function from C as this(somewhat simplified):
#define DLL_EXPORT __declspec(dllexport) __stdcall
typedef char DLL_IMPORT DCLINITIALIZE(char bDeviceId, char *pStr);
pString = "Some string";
DCLINITIALIZE * pfnInitialize = (DCLINITIALIZE*)GetProcAddress(hModule, "DCL_Initialize");
if (pfnInitialize)
pfnInitialize(1, pString);
The function doesn't return.
I think passing string to pascal routines from C may have special considerations.
My knowledge of pascal is very limited. and I don't know what is pAnsiChar;
Can anybody help me?
Edit:
I'm not sure the library is written in pascal. The only thing I know is its pascal prototype and that it works in a delphi application correctly.
pumpbin.exe /exports shows the names of funtions not capitalized
Edit2: string Software\Borland\Delphi\Locale found in DCL.dll several times, so it may be created by delphi
Edit3: The thread encounters a first chance exception and terminates. but the equivalent code in delphi works. the only thing to matter is passing char *sIP to
pascal function. is char * in C compatible to PAnsiChar in pascal?
Edit4: Visual studio 2013's debugger and delphi's debugger show different disassembly. Bellow is shown some part of both. The difference is marked by my comment.
/11th assembly instruction in DCL_Initialize loaded by delphi 7
0029B8E7 55 push ebp
0029B8E8 688ABA2900 push $688ABA2900
0029B8ED 64FF30 push dword ptr fs:[eax]
0029B8F0 648920 mov fs:[eax], esp
0029B8F3 33DB xor eax, eax 0029B8F5 C605F0FF290000 mov byte ptr [$0029FFF0], $00
0029B8FC 33C0 xor eax, eax
0029B8FE 55 push ebp
0029B8FF 6863BA2C00 push $002CBA63
0029B904 64FF30 push dword ptr fs:[eax]
0029B907 648920 mov fs:[eax], esp
0029B90A 8A4508 mov al, [ebp+$08]
0029B90D E8FEFEFFFF call -$00000102
0029B912 85C0 test eax, eax
002CB914 740F jz +$0f
004D52C7 55 push ebp
004D52C8 68 0E 55 4D 00 push 4D550Eh
004D52CD 64 FF 30 push dword ptr fs:[eax]
004D52D0 64 89 20 mov dword ptr fs:[eax],esp
004D52D3 C6 45 FF 00 mov byte ptr [ebp-1],0
004D52D7 C6 05 18 54 4E 00 00 mov byte ptr ds:[4E5418h],0
004D52DE C6 05 20 54 4E 00 00 mov byte ptr ds:[4E5420h],0
004D52E5 C6 05 19 54 4E 00 00 mov byte ptr ds:[4E5419h],0
004D52EC 33 C0 xor eax,eax
004D52EE 55 push ebp
004D52EF 68 E5 54 4D 00 push 4D54E5h
004D52F4 64 FF 30 push dword ptr fs:[eax]
004B52F7 64 89 20 mov dword ptr fs:[eax],esp
004B52FA 8A 45 08 mov al,byte ptr [ebp+8]
004B52FD E8 FA FD FF FF call 039B50FC
004B5302 85 C0 test eax,eax
004B5304 74 11 je 039B5317
And noticable is use of fs segment register in delphi dll. Is delphi's PE model is flat?
Edit5: dumpbin /disasm produces an assembly listing exactly matching that of C disassembly, different from disassembly of Delphi.