Carol Victor
Carol Victor

Reputation: 341

Can't Import MASM Function Into C

Using VS2019 (32-Bit Project)

What I tried:

Nothing seems to work, I keep getting the LNK2001 error.

Here is how I am doing it (With the actual assembly code removed, since it happens regardless of it):

Assembly file:

.model flat,stdcall
.stack 4096
.code

MyFunction proc
push ebp
mov ebp,esp
mov eax,[ebp+08h]
;...
pop ebp
ret 04h
MyFunction endp

end

C file:

extern void __stdcall MyFunction(unsigned int x);

Upvotes: 0

Views: 334

Answers (1)

Carol Victor
Carol Victor

Reputation: 341

I have fixed my issue.

It seems that in my case the fact that I did not decorate the instruction was not an issue, the issue was that MASM would not know how many parameters my function had so it the decorated symbol was _MyFunctionName@0.

I first added a prototype with parameters to my function as such: MyFunction proc stdcall x:DWORD

But then the problem was that MASM would generate its own prologue and epilogue for my function which would screw up the stack so I additionally added the following lines at the top of my file:

option prologue:none
option epilogue:none

And it now works perfectly.

Upvotes: 2

Related Questions