Click here to Skip to main content
15,887,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,

I am trying to call a subroutine in my Dll from VBA but keep getting the error "Run Time Error 49 Bad DLL Calling Convention". Any suggestions on what I am doing wrong?

<code>Declare Sub init_gen_rand Lib "SFMT.dll" (seed As Double)</code>

<code>__declspec( dllexport ) void init_gen_rand(uint32_t seed);</code>


In this other example, I am calling a function with no argument and do get a return number but the return numbers are sometimes negative even though the data type is an unsigned integer uint32_t. What is going wrong?

Declare Function gen_rand32 Lib "SFMT.dll" () As Double
<code>__declspec( dllexport ) uint32_t gen_rand32(void);</code>


Thanks for your help,
Neil
Posted

The VBA can succesfully import only function declared with the __stdcall calling convention; try this way:

VB
Declare Function gen_rand32 Lib "SFMT.dll" () As Long

C++
__declspec( dllexport ) uint32_t __stdcall gen_rand32(void);
 
Share this answer
 
Comments
Neil Sheridan 23-Jul-10 5:13am    
Thanks! That fixed the calling question. I am still getting negative numbers but I am assuming that if something meant for an UINT32_t is instead passed into a Long then when I will end up keeping the sign. Therefore I will have to make the number absolute somewhere else in the code.
Thanks, any suggestions? I have also tried Long with the same result.

Declare Function gen_rand32 Lib "C:\Users\Neil\Documents\Visual Studio 2010\Projects\SFMT\Release\SFMT.dll" () As Long
Declare Sub init_gen_rand Lib "C:\Users\Neil\Documents\Visual Studio 2010\Projects\SFMT\Release\SFMT.dll" (seed As Long)
 
Share this answer
 
A uint32 is not a double. I'd say that's the issue.
 
Share this answer
 
Comments
Dalek Dave 22-Jul-10 18:22pm    
Yep, I concur with thee oh mighty one.
Seems reasonable. Damn typing! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900