Strange DLL problem...

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 3
Reputation: daggath is an unknown quantity at this point 
Solved Threads: 0
daggath daggath is offline Offline
Newbie Poster

Strange DLL problem...

 
0
  #1
Sep 6th, 2008
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
  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
  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Strange DLL problem...

 
0
  #2
Sep 6th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Strange DLL problem...

 
0
  #3
Sep 6th, 2008
The VB declare statement should be:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 3
Reputation: daggath is an unknown quantity at this point 
Solved Threads: 0
daggath daggath is offline Offline
Newbie Poster

Re: Strange DLL problem...

 
0
  #4
Sep 6th, 2008
Originally Posted by dougy83 View Post
The VB declare statement should be:
  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...

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Strange DLL problem...

 
0
  #5
Sep 6th, 2008
Originally Posted by Ancient Dragon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Strange DLL problem...

 
0
  #6
Sep 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 3
Reputation: daggath is an unknown quantity at this point 
Solved Threads: 0
daggath daggath is offline Offline
Newbie Poster

Re: Strange DLL problem...

 
0
  #7
Sep 6th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC