Accessing a DLL function
Accessing a DLL function
(OP)
I've beem trying for a week to access a dll function while running Excel/VBA. This function is contained in a 3rd party dll (not a MS product). The name of this old DLL is "sai.dll" I believe it was written in C around 1998-2000. After trying all the calling options using almost all the longs and pointers and StrPtr() and VarPtr() values I can permutate, I get error messages ranging from "Cannot find cdr entry point in the dll" to a total shutdown of Excel.
LIBRARY "Sai"
The Library export list shows only
cdr_ @1
The only description of the function in the documentation is,
void cdr(const char *case, int *ier)
The Arguments,
*case In VBA this will be a string representing an analysis case name, such as "SAramcoThermal" or similar
*ier An error value, as returned by cdr(), supposedly 0, -1, etc.
In the documented descriptions, the function is referred to as "cdr", yet in the export list and a disassembler view it shows us as "cdr_", note the trailing underscore, which confuses me. What's that about?
And my real question is, does anybody have any ideas on how I should declare this function in VBA/Excel, and perhaps make a class wrapper or do something that will give Excel that returned value?
In any case, thanks for having a look.
LIBRARY "Sai"
The Library export list shows only
cdr_ @1
The only description of the function in the documentation is,
void cdr(const char *case, int *ier)
The Arguments,
*case In VBA this will be a string representing an analysis case name, such as "SAramcoThermal" or similar
*ier An error value, as returned by cdr(), supposedly 0, -1, etc.
In the documented descriptions, the function is referred to as "cdr", yet in the export list and a disassembler view it shows us as "cdr_", note the trailing underscore, which confuses me. What's that about?
And my real question is, does anybody have any ideas on how I should declare this function in VBA/Excel, and perhaps make a class wrapper or do something that will give Excel that returned value?
In any case, thanks for having a look.
Independent events are seldomly independent.
RE: Accessing a DLL function
TTFN
FAQ731-376: Eng-Tips.com Forum Policies
Need help writing a question or understanding a reply? forum1529: Translation Assistance for Engineers
RE: Accessing a DLL function
new user/join/password ... not a valid user... new user/join/password .... nth time thing.
OK. I'll try it.
Independent events are seldomly independent.
RE: Accessing a DLL function
The reference for what you want is in MSDN
So, at a guess, in your case, it would be
CODE
RE: Accessing a DLL function
Thanks a lot for that information.
I think the @1 above means the function is the first ordinal. It is the only one as well.
That Dependency Walker is a killer! I'm suffering from info overload right now, but it's great!
It shows the export function name is cdr_
I will try that declaration and let you know what happens.
Thanks again.
Independent events are seldomly independent.
RE: Accessing a DLL function
Public Declare Function cdr Lib "d:\sai.dll" _
Alias "cdr_@1" (ByVal casename As String, ByRef ier As Integer)
dim strConnection as string
dim ier as integer
I called it as,
Ret = cdr(strConnection, ier)
Result was,
Run-time error '453': Can't find DLL entry point cdr_@1 in d:\sai.dll
VBA would only accept me writing the call as "cdr" not by the aliased name "cdr_@1".
This isn't one of those cases where strConnection has to be 144 characters long and terminated in a null, or is it? Well, the "can't find entry point" seems like a different problem then not recognizing sring variables.
Independent events are seldomly independent.
RE: Accessing a DLL function
Independent events are seldomly independent.
RE: Accessing a DLL function
CODE
RE: Accessing a DLL function
Thanks for the continued support.
Independent events are seldomly independent.
RE: Accessing a DLL function
At least it didn't knock XL down this time.
Independent events are seldomly independent.
RE: Accessing a DLL function
CODE
RE: Accessing a DLL function
Is not the ier variable returned, indicating the status of the connection?
Independent events are seldomly independent.
RE: Accessing a DLL function
RE: Accessing a DLL function
Independent events are seldomly independent.
RE: Accessing a DLL function
- http://support.microsoft.com/kb/187912
- http://msdn.microsoft.com/en-us/library/office/bb6...
- http://support.microsoft.com/kb/106553/en-us
Looks like it is something to do with ByVal casename As String.The problem is it is expecting a BSTR or wchar_t* but you've got a char*. One option is to write another DLL that calls your DLL. This will convert the wchar_t* to a char* and then pass it into cdr.
RE: Accessing a DLL function
Thanks again xwb.
I'd give you another star if I could.
You're as persistent as I am.
Independent events are seldomly independent.
RE: Accessing a DLL function
Independent events are seldomly independent.
RE: Accessing a DLL function
A C Integer is equivalent to a VBA Long, so integers should be passed as Longs.
See: http://www.drivenbysteam.com/?p=158 for a table of equivalent data types.
The pages from my blog linked here: http://newtonexcelbach.wordpress.com/2012/10/16/da...
may be of some use.
They are written on the assumption that you have access to the C code, but there may be something in there that helps solve the problem.
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: Accessing a DLL function
The problem (I think) turned out to be trying to run a 32 bit direct memory access routine on Win7. The function crashes on Win7. Works on my oldest laptop (XPSP3) when running with Visual Basic 6. Now I must try it with VBA. I may need those variable converts as well. Thanks for that Doug.
XLB your help is greatly appreciated.
Independent events are seldomly independent.