Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing a DLL function 1

Status
Not open for further replies.

BigInch

Petroleum
Jun 21, 2006
15,161
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.

Independent events are seldomly independent.
 
Replies continue below

Recommended for you

You know, we usually send questions like this over to Tek-Tips, so that's probably still a value suggestion.

TTFN
faq731-376
7ofakss

Need help writing a question or understanding a reply? forum1529
 
Then I'll be forced to try the
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.
 
The library export list doesn't look right. If you have two parameters, it should be cdr_@8. The number after the @ is in multiples of 4. You should get @0 for no parameters, @4 for 1 parameter etc. If cdr_@1 is from the documentation, check it using depends (
The reference for what you want is in MSDN

So, at a guess, in your case, it would be
Code:
declare cdr lib "sai.dll" alias "cdr_@1" (byval casestr as string, byref ier as integer)
 
xwb,
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.
 
I declared and called the 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.
 
No I figured out that it's not one of those null terminated strings. As the function does not return a string, I don't require that string to accept it.

Independent events are seldomly independent.
 
If the function name is cdr_, just change the declaration to
Code:
Public Declare Function cdr Lib "d:\sai.dll" _
Alias "cdr_" (ByVal casename As String, ByRef ier As Integer)
 
OK. I'll try it.
Thanks for the continued support.

Independent events are seldomly independent.
 
Tried it. "Bad calling convention"
At least it didn't knock XL down this time.

Independent events are seldomly independent.
 
It is a void function so maybe the declaration should be
Code:
Public Declare sub cdr Lib "d:\sai.dll" _
Alias "cdr_" (ByVal casename As String, ByRef ier As Integer)
If it is a function, a return type has to be specified.
 
Thanks. I'll try anything at this point. I'm considering learning C!!!
Is not the ier variable returned, indicating the status of the connection?

Independent events are seldomly independent.
 
Yes but it is returned as a parameter: not as the result of the function.
 
Again the "Bad calling convention" error was raised. [banghead]

Independent events are seldomly independent.
 
Have a look at the following
[ul]
[li][/li]
[li][/li]
[li][/li]
[/ul]
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.
 
I was thinking before that it might have been a BSTR or some string to long conversion problem and tried a number of the conversions. I'll try again, this time using these latest declare formats and see if it produces some results.

Thanks again xwb.
I'd give you another star if I could.
You're as persistent as I am.

Independent events are seldomly independent.
 
Do you think VB6 will have the same variable issues as VBA?

Independent events are seldomly independent.
 
Some suggestions that may (or may not) help:

A C Integer is equivalent to a VBA Long, so integers should be passed as Longs.

See: for a table of equivalent data types.

The pages from my blog linked here: 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
 
Sorry about the delay posting the outcome.
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor