954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Strange DLL problem...

Hi, I recently was trying to learn how to write DLLs and was doing ok until I ran across this interesting problem. It seems that the function is not receiving the last argument.


Here is my code for the dll

#include "stdafx.h"

BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    return TRUE;
}

_declspec (dllexport) int _stdcall sum(int x , int y)
{
	return (x+y);
}


and here is my .def file

LIBRARY example1

EXPORTS

sum	@1


I am using visual studio C++ 2005 to write the dll and am using VB to call it. When I call sum it returns the value of x.. if I tell the dll to return y I always get 0 ... I can't figure it why. When I added more arguments to the function I found that it was always the last variable that always is received as 0. What is happening? What am I doing wrong?

daggath
Newbie Poster
3 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

how did you declare it in your vb program? what you posted appears to be correct. But you don't need both the *.def file and use of _dllspec(__export) -- all you need is one of them.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

The VB declare statement should be:

declare function sum lib "example1.dll" (byval x as long, byval y as long) as long


where example1.dll is the name of your dll file.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

The VB declare statement should be:

declare function sum lib "example1.dll" (byval x as long, byval y as long) as long
where example1.dll is the name of your dll file.

yes that is what I basicly have...

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Button1.Text = Str(sum(CInt(TextBox1.Text), CInt(TextBox2.Text)))
    End Sub
 
    Private Declare Function sum Lib "example1.dll" (ByVal x As Long, ByVal y As Long) As Long
 
End Class


but I still have that problem

daggath
Newbie Poster
3 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 
how did you declare it in your vb program? what you posted appears to be correct. But you don't need both the *.def file and use of _dllspec(__export) -- all you need is one of them.

You do need both the *.def file and use of _dllspec(__export) if you want clean names exported from the dll (at least in MinGW and VC++2008). Without the def file, you will export a function something like "?sum@@YGHHH@Z"; with the .def file the function name exported is "sum"

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

What language are you using to call the dll?? You said VB above (I assumed vb6), are you sure it's not VB.net or Visual Basic 2005/6/7/8?? vb6 successors have different data types to vb6 - I think long is 64bits, so use Int32 instead of long for the declare statement.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

Sorry I wasn't clear, it was visual basic 2005... I'll try your suggestion of using int32.. will update this post when I finish trying it...

Yes that worked!

Thank you very much :-)

daggath
Newbie Poster
3 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You