News:

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

Main Menu

trouble with external variables

Started by oinka720, July 28, 2017, 12:50:19 PM

Previous topic - Next topic

oinka720

Hi all.  This ought to be simple but I can't figure it out.  I have a test dll written in c++

// MyDLL.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"


extern "C" __declspec( dllexport ) public int MyNum = 5;


and then I have a test asm where I try to access the variable

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

;                 Build this with the "Project" menu using
;                       "Console Assemble and Link"

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

    .486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\msvcrt.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\msvcrt.lib
   
   
    includelib mydll.lib

    .code                       ; Tell MASM where the code starts

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

start:                          ; The CODE entry point to the program

;mov MyNum, 5

    push MyNum
    push offset format
    call crt_printf
   
    exit

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


.data

format db 'MyNum is %i', 13,10,0
extern MyNum:dword

end start                       ; Tell MASM where the program ends




it assembles and links, but ...

Problem1: it prints a random number rather than the expected 5.
Problem2: if I uncomment the line ;mov MyNum, 5 I get a runtime error "access violation"

Any assistance is appreciated.  Thanks.

hutch--

I don't know if C++ decorates the external function name but I would try STDCALL as the calling convention.

aw27

#2
In DLL:
extern "C" const int MyNum = 5;
In .DEF
LIBRARY   MYDLL
EXPORTS 
  MyNum = MyNum CONSTANT

In MASM:

.686
.model Flat, STDCALL
option Casemap :None

includelib \masm32\lib\msvcrt.lib
printf proto C :ptr, :vararg
includelib mydll.lib
externdef MyNum : sdword

.data

format db 'MyNum is %d', 13,10,0

.code

start1 proc public
mov edx, MyNum
mov edx, [edx]
INVOKE printf, addr format, edx
ret
start1 endp

main proc C public
invoke start1
ret

main endp
end


Tested and works.

TWell

An another example: .386
.model flat, stdcall
option casemap :none

includelib msvcrt
printf proto C :ptr byte, :vararg
exit proto C :dword

includelib MyDll1
extern _imp__MyNum :dword

.data
fmt db "%p",10,0
MyNum1 dd ?

.code
mainCRTStartup proc C
mov eax, _imp__MyNum
mov eax, dword ptr[eax]
mov MyNum1, eax
invoke printf,addr fmt, MyNum1
invoke exit,0
mainCRTStartup endp
end
00000005

oinka720

Thanks for the examples and advice.  It's working now  :t  Until the next roadblock...