News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

ABOUT DLL LIBRARY

Started by Force, August 15, 2014, 10:20:33 AM

Previous topic - Next topic

Force

i can use in Masm32 if i create DLL library with  C

Masm32 Code :
.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib  mydll.lib

result PROTO:DWORD,:DWORD,:DWORD

.data
test1 db "Masm",0
test2 db " 32",0
buffer db 30 dup(0)

.code
start:
invoke result,addr test1,addr test2,addr buffer

invoke MessageBox,NULL,addr buffer,0,0

invoke ExitProcess,NULL
end start


C  DLL Code :



#include <windows.h>

BOOL APIENTRY DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:  break;
        case DLL_THREAD_ATTACH:   break;
        case DLL_THREAD_DETACH:   break;
        case DLL_PROCESS_DETACH:  break;
    }
    return TRUE;
}

__declspec(dllexport) int  __cdecl result(char *aaa,char *ccc,char *ek) {
strcpy(ek,aaa);
strcat(ek," ");
strcat(ek,ccc);
strcat(ek," \n");
strcat(ek,"C DLL");

return 0;
}


but after creating a Dll library with Masm32 i cant use it in C

C Code :


#include <windows.h>

typedef int    (__cdecl *ProcAddress_1)(char*,char*,char*);

int WINAPI
    WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
   HINSTANCE testDll = LoadLibrary("mydll.dll");
ProcAddress_1 result =(ProcAddress_1) GetProcAddress(testDll, "result");

char txt1[]="Coming";
char txt2[]="From C";
char rslt [50];


result(txt1,txt2,rslt);

MessageBox(NULL,rslt,0,0);


FreeLibrary(testDll);

    return 0;
}


Masm32 Dll code
.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

.data

test2 db "ASSEMBLER",0

.code


start:


startProc proc hinstDLL:DWORD, fdwReason:DWORD, lpvReserved:DWORD
        mov     eax, TRUE
        ret
startProc endp

result proc dnm:LPSTR,dnl:LPSTR,buf:LPSTR
invoke lstrcpy,buf,dnm
invoke lstrcat,buf,dnl
invoke lstrcat,buf,addr test2
ret
result endp

end start


Do I make mistake in C or Assembly ?

x64Core

Are you sure you are exporting the symbol? It is my opinion the easiest way is using a .def file:
LIBRARY Project_Name
EXPORTS
result

then check with a PE viewer and make sure you are following the same calling convention:
typedef int    (__cdecl *ProcAddress_1)(char*,char*,char*);

hutch--

Force,

There should be no problems running a DLL created in MASM from C. The main things you must get right are using the same calling convention and prototyping your C code as EXTERN C as per the documentation in the C compiler you use. I am referring to 32 bit code.

GoneFishing

Quote from: Force on August 15, 2014, 10:20:33 AM
...
Do I make mistake in C or Assembly ?
Your asm code doesn't have necessary  LibMain proc . Check DLL example in masm32 examples folder.



Force

I use Masm32 DLL library with Fasm  there is not any problem ... i just got  problem with C program i compiled it with Pelles C maybe i need to try other C compilers

sinsi

With this line ".model flat,stdcall" you are making all procs STDCALL. Maybe use
result proc C dnm:LPSTR,dnl:LPSTR,buf:LPSTR
Not knowing C I am assuming that by default everything is C calling convention in a C program.

Force

after writing this line in C code
typedef int    (__stdcall *ProcAddress_1)(char*,char*,char*);

instead of this line
typedef int    (__cdecl *ProcAddress_1)(char*,char*,char*);

i can use Masm32 DLL library in  C programs ,

does anybody know what difference is between stdcall and cdecl ?




hutch--

Yes, its reasonably simple, C pushes a variable number of arguments, runs the proc then balances the stack after the proc exits. STDCALL uses a fixed number of arguments and balances the stack before it returns to the calling code.

Force

Thanks Hutch i got it now ...