Author Topic: trouble with external variables  (Read 318 times)

oinka720

  • Regular Member
  • *
  • Posts: 2
trouble with external variables
« on: July 28, 2017, 12:50:19 PM »
Hi all.  This ought to be simple but I can't figure it out.  I have a test dll written in c++

Code: [Select]
// 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

Code: [Select]
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

;                 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--

  • Administrator
  • Member
  • ******
  • Posts: 4930
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: trouble with external variables
« Reply #1 on: July 28, 2017, 01:04:03 PM »
I don't know if C++ decorates the external function name but I would try STDCALL as the calling convention.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

aw27

  • Member
  • ****
  • Posts: 854
  • Let's Make ASM Great Again!
Re: trouble with external variables
« Reply #2 on: July 28, 2017, 04:33:09 PM »
In DLL:
extern "C" const int MyNum = 5;
In .DEF
LIBRARY   MYDLL
EXPORTS 
  MyNum = MyNum CONSTANT

In MASM:
Code: [Select]
.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.
« Last Edit: July 28, 2017, 09:52:01 PM by aw27 »

TWell

  • Member
  • ****
  • Posts: 748
Re: trouble with external variables
« Reply #3 on: July 28, 2017, 05:46:12 PM »
An another example:
Code: [Select]
.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
Code: [Select]
00000005

oinka720

  • Regular Member
  • *
  • Posts: 2
Re: trouble with external variables
« Reply #4 on: August 05, 2017, 09:15:11 AM »
Thanks for the examples and advice.  It's working now  :t  Until the next roadblock...