News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Making and Using a DLL with Assembly program

Started by Robert Collins, December 19, 2004, 10:00:46 PM

Previous topic - Next topic

John

#15
What is command line you use to assemble the ASM portion?

Make sure you are using the /Cx and /Cp options.

Also, why not use the invoke syntax, is that another restriction?
I've never seen it done the way you are doing it, but I'm pretty new to all of this anyway  :red

tenkey

Quote from: Robert Collins on December 21, 2004, 04:00:55 AM

void _stdcall ShowMessageBox()
{
  MessageBox (0,"HELLO FROM DLL","MYDLL.DLL",MB_OK);
  return;
}


... snip ...

Now, when I assemble and link the above .asm I always get that 'unresolved external 'ShowMessageBox@0' message.

If the DLL was created using MS VC++ as a '.c' program(instead of a .cpp) and the #include stdafx.h statement removed then the output MyDLL2.dll and MyDLL2.lib files will work as is in the above Assembly program but not if the DLL is compiled as a C++ program. But the problem remains that the programmers in the System Programming are always going to make DLLs using .cpp files and not .c files. So, tha't my situation.

In VC++, you need to add extern "C".


extern "C" void _stdcall ShowMessageBox()
{
  MessageBox (0,"HELLO FROM DLL","MYDLL.DLL",MB_OK);
  return;
}


If you have examples of the DLL's or its functions' declarations and uses in VB 6, it will be more revealing. You only need to supply one function that has arguments.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

Robert Collins

Quote from: tenkey on December 21, 2004, 07:33:43 AM

In VC++, you need to add extern "C".


extern "C" void _stdcall ShowMessageBox()
{
  MessageBox (0,"HELLO FROM DLL","MYDLL.DLL",MB_OK);
  return;
}

.

Damn! You know I tried that the other day but instead of putting the extern "C" as part of the function I put it at the top of the file thinking that's where it should be so that it would be global for all functions, but it didn't work so I just abandoned that thought. It didn't occur to me to put it in the function. Thanks, tenkey, that solves my problem.

Relvinian

The other thing you can do to make sure you have "extern 'c'" prototype for all your .asm functions which are called from VC++ is create a header file with something like the following:

// make all functions standard "C" names and not C++ mangled.
#ifdef __cplusplus
extern "C"
{
#endif


// prototype goes here
void MyFunc();
void MyFuncWithArg(int argc);
/* etc, etc, etc */


// end specific C names
#ifdef __cplusplus
}
#endif


Then anytime you need to use one of your ASM functions, just include the appropriate header file (where you have the function declared).

Relvinian

Robert Collins

OK, guys, thanks to each and everyone of you I can now sleep. I convinced the Systems Programming Dept, those who make the DLLs in VC++, to include a .h file as indicated above by Relvinian. This will solve the problem for all DLLs this day forward but not so for all them that have already been written. I suppose that if the company wants to develop certain applications in Assembly then someone is just going to have to go back and re-compile the older DLLs. Probably on a need to do basis.

Anyway, that's their problem.

Thanks to all for your help. 

Vortex

Hi Robert,

Did you check my second example?  ( the new attachment )

kidteam

help me
i'm created 2 files :
  - file DLL
  - file LIB
i create by MASM32
how to use it in the C++
i'm using dev-c++ thank
file cong.asm
==============================
cong proc a:DWORD
  mov eax,a
  inc eax
  ret
cong endp
==============================
file cong.def
==============================
LIBRARY cong
EXPORTS cong
==============================
file CPP
===========================
DLLIMPORT int cong(int);
int main(void)
  {
    int a=10,b;
    b=cong(a);
  }
===========================
link error

UtillMasm

lib file?
how you created?

this way?
    rc.exe UtillMasm.rc
    ml.exe /c /coff UtillMasm.asm
    lib.exe /subsystem:windows UtillMasm.obj

###
or that way?

Vortex

Quotei'm using dev-c++ thank

Are you referring to Bloodshed Dev-C++ ?

UtillMasm


kidteam

help me
i need use masm32 to create file KIDTEAMDLL.DLL
after using in Dev-C++
code
file KIDTEAMDLL.ASM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    ; -------------------------------------------
    ; Build this DLL with the provided MAKEIT.BAT
    ; -------------------------------------------

      .data?
        hInstance dd ?

      .code

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

LibMain proc instance:DWORD,reason:DWORD,unused:DWORD

    .if reason == DLL_PROCESS_ATTACH
      mrm hInstance, instance       ; copy local to global
      mov eax, TRUE                 ; return TRUE so DLL will start

    .elseif reason == DLL_PROCESS_DETACH

    .elseif reason == DLL_THREAD_ATTACH

    .elseif reason == DLL_THREAD_DETACH

    .endif

    ret

LibMain endp


kidteam proto
kidteam proc
  jmp qua
    nd BYTE "Ngo Hung Cuong",0
    td BYTE "Kidteam",0
  qua:
  invoke MessageBox,0,addr nd,addr td,MB_OK
  ret
kidteam endp

end LibMain
============================================
file makeit.bat
@echo off
if exist kidteamdll.obj del kidteamdll.obj
if exist kidteamdll.dll del kidteamdll.dll
\masm32\bin\ml /c /coff kidteamdll.asm
\masm32\bin\Link /SUBSYSTEM:WINDOWS /DLL /DEF:kidteamdll.def kidteamdll.obj
del kidteamdll.obj
del kidteamdll.exp
dir kidteamdll.*
pause
====================================================
file KIDTEAMDLL.DEF
LIBRARY kidteamdll
; EXPORTS [your_exported_proc_name]
EXPORTS kidteam
=====================================================
file testDLL.CPP
#include <windows.h>
#define DllImport extern "C" __declspec (dllimport)
DllImport void kidteam(void);
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

  {
    kidteam();
    return 0;
  }
but compiler error
  [Linker error] undefined reference to `_imp__kidteam'

hutch--

For MASM to recognise an external procedure you need to prototype it in MASM format taking notice of the calling convention you use.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MichaelW

kidteam,

It looks like you are trying to create a DLL with MASM and use it from a CPP app. Your makeit.bat should be creating the import library kidteamdll.lib. You need to link this import library with your test app.

eschew obfuscation

drizz

default calling convention for masm32 is stdcall (as per "masm32rt.inc" ".module flat,stdcall")
default calling convention for any cpp compiler is cdecl

cdecl != stdcall

solution 1
kidteam proc c

solution 2
extern "C" __declspec (dllimport)  void __stdcall kidteam(void);

use either "solution 1" or "solution 2", not both.
The truth cannot be learned ... it can only be recognized.

kidteam