Solved

How do I Call a function form a dll file

Posted on 2007-12-02
9
207 Views
Last Modified: 2008-02-01
I made a simple dll file from http://www.icynorth.com/development/createdlltutorial.html but i get the error Bad DLL calling convention when I try to use it. Can someone show me the correct way to use this dll from VB.

Thanks for your help
rich
0
Comment
Question by:rcherne1
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 20391791
Hello rich,

How are you trying to call it now?

Regards,

Patrick
0
 

Author Comment

by:rcherne1
ID: 20391804

Private Declare Function RetrieveTempFilename Lib "c:\work\MakeTempFilenameDLL.dll" (szDirectory As String, szPrefix As String) As String

Private Sub Command1_Click()
Dim my_szDirectory As String
Dim my_szPrefix As String
my_szDirectory = "c:\work\"
my_szPrefix = "tst"
MsgBox RetrieveTempFilename(my_szDirectory, my_szPrefix)
End Sub
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20391982
can you show how exactly you defined the function in your dll code?
0
 

Author Comment

by:rcherne1
ID: 20391997

char szReturn[MAX_PATH];
 

__declspec(dllexport) LPCTSTR RetrieveTempFilename(LPCTSTR szDirectory, LPCTSTR szPrefix)

{

     char szBuffer[MAX_PATH];

     if(GetTempFileName(szDirectory,szPrefix,0,szBuffer) == 0)

     {

          // It failed - return the string "ERROR"

          lstrcpy(szReturn,"ERROR");

     } else {

          // Success!

          lstrcpy(szReturn,szBuffer);

     }

     return szReturn;

}

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 20392030
please try to change the declaration:


Private Declare Function RetrieveTempFilename Lib "c:\work\MakeTempFilenameDLL.dll" (byval szDirectory As String, byval szPrefix As String) As String

0
 
LVL 22

Assisted Solution

by:danaseaman
danaseaman earned 250 total points
ID: 20392042
VB6 can only call __stdcall functions, not __cdecl functions so you will need to change this to __stdcall.

Error is "Bad DLL calling convention"

   __declspec(dllexport) LPCTSTR RetrieveTempFilename(LPCTSTR szDirectory, LPCTSTR szPrefix)

Try changing to this and recompile the DLL:
   __stdcall(dllexport) LPCTSTR RetrieveTempFilename(LPCTSTR szDirectory, LPCTSTR szPrefix)

0
 

Author Comment

by:rcherne1
ID: 20392282
Hum? That wont compile but I changed it to
"LPCTSTR __stdcall  RetrieveTempFilename(LPCTSTR szDirectory,LPCTSTR szPrefix)"
Now I dont get an error but i think its returning a pointer to the string not a string.

Sound like I should posted this in the vc++ area to


0
 
LVL 22

Expert Comment

by:danaseaman
ID: 20392307
I probably have the wrong syntax (I don't use C++):
http://www.mingw.org/MinGWiki/index.php/VB-MinGW-DLL
http://www.imc.org/imc-vcard/mail-archive/msg00353.html
http://www.eee.bham.ac.uk/collinst/VisualBasic-VisualC-DLL.html

Perhaps it should be:
__declspec(dllexport) __stdcall LPCTSTR RetrieveTempFilename(LPCTSTR szDirectory, LPCTSTR szPrefix)

0
 

Author Comment

by:rcherne1
ID: 20392968
angelIII and danaseaman you both had part of my answer.
I had to pass my string byval and use __stdcall
I also found that it returns a pointer.

Thanks for your help.
Rich
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now