943,662 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 761
  • C++ RSS
Sep 6th, 2008
0

Strange DLL problem...

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2.  
  3. BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  4. {
  5. return TRUE;
  6. }
  7.  
  8. _declspec (dllexport) int _stdcall sum(int x , int y)
  9. {
  10. return (x+y);
  11. }

and here is my .def file
C++ Syntax (Toggle Plain Text)
  1. LIBRARY example1
  2.  
  3. EXPORTS
  4.  
  5. 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daggath is offline Offline
3 posts
since Sep 2008
Sep 6th, 2008
0

Re: Strange DLL 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Sep 6th, 2008
0

Re: Strange DLL problem...

The VB declare statement should be:
VB Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Sep 6th, 2008
0

Re: Strange DLL problem...

Click to Expand / Collapse  Quote originally posted by dougy83 ...
The VB declare statement should be:
VB Syntax (Toggle Plain Text)
  1. 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...

C++ Syntax (Toggle Plain Text)
  1. Public Class Form1
  2.  
  3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.  
  5. Button1.Text = Str(sum(CInt(TextBox1.Text), CInt(TextBox2.Text)))
  6. End Sub
  7.  
  8. Private Declare Function sum Lib "example1.dll" (ByVal x As Long, ByVal y As Long) As Long
  9.  
  10. End Class

but I still have that problem
Last edited by daggath; Sep 6th, 2008 at 8:18 pm. Reason: clicked on post to soon.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daggath is offline Offline
3 posts
since Sep 2008
Sep 6th, 2008
0

Re: Strange DLL 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"
Last edited by dougy83; Sep 6th, 2008 at 8:28 pm.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Sep 6th, 2008
0

Re: Strange DLL problem...

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.
Last edited by dougy83; Sep 6th, 2008 at 8:33 pm.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Sep 6th, 2008
0

Re: Strange DLL problem...

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 :-)
Last edited by daggath; Sep 6th, 2008 at 8:49 pm. Reason: tested suggestion
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daggath is offline Offline
3 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Staggered input from file
Next Thread in C++ Forum Timeline: array help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC