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?

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.

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.

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

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"

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.

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 :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.