Calling a C dll function with VB

wym used Ask the Experts™
I have written a C dll that exports functions that return LPXXX variables.  


LPMAPISESSION __stdcall GetSession();
void  __stdcall CloseSession(LPMAPISESSION);

I want to be able to call these functions from VB, so I declared them like this:

Private Declare Function GetSession Lib "lgkmapi.dll" () As Long
Private Declare Function CloseSession Lib "lgkmapi.dll" (ByVal lpSession As Long)

Private Sub Command1_Click()

Dim lpSession As Long

lpSession = GetSession()
MsgBox lpSession
CloseSession (lpSession)
End Sub

My problem is that I'm crashing on the CloseSession function.  

I debug it through VC and I see that the value of lpSession returned from GetSession is 0x0168f108.  From VB, the message box shows 23654664 was returned, which is the equivalent of 168f108 in hex.

When I debug the CloseSession function in VC, I see that I'm actually passing in 0x0063f3e8 for lpSession.  How'd this happen?  I have tried passing lpSession ByRef and ByVal in CloseSession but neither works.  

Can anyone help solve this?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You might want to check that you are exporting the function names 'undecorated'. You can check this by typing "dumpbin lgkmapi.dll /exports" from the dos prompt. If your names are being correctnly exported you should see then as you call them from VB - if they decorated you will see characters before and after the function name as well as the names of the variables you are passing.

To get VB to interpret the calls correctly I woudl add a .def file in the project which would look like this

LIBRARY lgkmapi.dll


This should ensure that your functaion are exported in a form that VB understands.

Hope this helps  
What is typedef for LPMAPISESSION?


Well, I found my problem was the fact that I was using an older version of my dll (in the same folder as my VB exe) and I should have given it an absolute path of my debug folder in my VC project when I stepped through in VC.  (DOH!)  But, I'm giving you the points because I learned something new about the dos prompt dumpbin which was neat.  

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial