[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


How do I Call a function form a dll file

Posted on 2007-12-02
Medium Priority
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
Question by:rcherne1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
LVL 93

Expert Comment

by:Patrick Matthews
ID: 20391791
Hello rich,

How are you trying to call it now?



Author Comment

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
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20391982
can you show how exactly you defined the function in your dll code?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

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"
     } else {
          // Success!
     return szReturn;

Open in new window

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 1000 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

LVL 22

Assisted Solution

danaseaman earned 1000 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)


Author Comment

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

LVL 22

Expert Comment

ID: 20392307
I probably have the wrong syntax (I don't use C++):

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


Author Comment

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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

649 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