Search code examples
cassemblydllglobal-variablesmasm

MASM: Accessing to global C variable from assembly


I'm writing a program to convert image and compare the speed of processing data in C and assembly. I have 3 projects:

  • main project in C
  • DLL in C to convert image
  • DLL in ASM to convert image

In C DLL header, I simply wrote:

#ifdef PROJEKTC_EXPORTS
    #define PROJEKTC_API __declspec(dllexport)
#else
    #define PROJEKTC_API __declspec(dllimport)
#endif

...

extern PROJEKTC_API unsigned int ThreadID;      

PROJEKTC_API void __cdecl funkcjaC(void* Args); 

and after including this header, I can access variable ThreadID both in main project and C DLL.

The problem starts when I try to do the same in ASM. I tried constructions like extern ASMThreadID:dword in .code block, but it won't work.

The error I got: error LNK2019: unresolved external symbol _ASMThreadID referenced in function _MyProc1

I have a feeling that it's a matter of 1-2 lines of code, but I can't figure out which instruction should I use.

I link the projects by module definition file in ASM and adding ASM.lib file into the Linker->Input of main project.

Do you have any suggestions?


Solution

  • With small help from old posts in asmcommunity.net, I managed to get it working:

    • In .asm file, before .data segment:

    EXTERNDEF C ASMThreadID:DWORD

    • In .data segment:

    ASMThreadID dd 0

    • In .def file of ASM DLL:

    LIBRARY "nameOfProject" EXPORTS ... ASMThreadID

    • In main C program header (like global declaration):

    extern __declspec(dllimport) unsigned int ASMThreadID;

    Now it works like a charm.

    The 'public' declaration sent me to the right way of searching. Thanks for your help, mate!