c++cdllnsis

How to Use SYSTEM::CALL in NSIS Script?


I'm trying to call a function from a DLL in my NSIS script using SYSTEM::CALL. Here's the code I'm working with:

Section loadDll
    StrCpy $0 10
    StrCpy $1 5 
    System::Call "$INSTDIR\NSISRegistryTool::Add(i, i) i(r0, r1).r2"
    MessageBox MB_OK "Return value: $0, $1, $2"
    SetPluginUnload manual
    System::Free 0
SectionEnd

I have confirmed that the Add function is correctly exported from NSISRegistryTool.dll, but $2 is empty after the call.

The function declaration in the DLL is as follows:

extern "C" __declspec(dllexport) int Add(int a, int b) 
{
    return a + b;
}

Could someone help me understand what I'm doing wrong or how to properly use SYSTEM::CALL to retrieve the return value from the DLL function? Thank you!

I modified the code to look like this, but $2 is still empty.

Section loadDll
    StrCpy $0 7
    StrCpy $1 4 
    System::Call "$INSTDIR\NSISRegistryTool::Add(i $0, i $1) i .r2"
    MessageBox MB_OK "Return value: $0,$1,$2"
    SetPluginUnload manual
    System::Free 0
SectionEnd

enter image description here


Solution

  • From the documentation:

    Libraries in the system32 directory can be loaded without a path. All other libraries should be loaded with a quoted full path.

    Section loadDll
        StrCpy $0 10
        StrCpy $1 5 
        System::Call '"$INSTDIR\NSISRegistryTool.dll"::Add(i r0, i r1)i.r2'
        MessageBox MB_OK "Return value: $2 from ($0, $1)"
    SectionEnd
    

    If your .dll is 32-bit and uses cdecl instead of stdcall, it needs to be System::Call '"$INSTDIR\NSISRegistryTool.dll"::Add(i r0, i r1)i.r2 ?c' instead.