INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# 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.

Independent events are seldomly independent.

### RE: Accessing a DLL function

(OP)
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.

### RE: Accessing a DLL function

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 (http://www.dependencywalker.com/)

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)

### RE: Accessing a DLL function

(OP)
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.

### RE: Accessing a DLL function

(OP)
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.

### RE: Accessing a DLL function

(OP)
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.

### RE: Accessing a DLL function

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) 

### RE: Accessing a DLL function

(OP)
OK. I'll try it.
Thanks for the continued support.

Independent events are seldomly independent.

### RE: Accessing a DLL function

(OP)
At least it didn't knock XL down this time.

Independent events are seldomly independent.

### RE: Accessing a DLL function

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.

### RE: Accessing a DLL function

(OP)
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.

### RE: Accessing a DLL function

Yes but it is returned as a parameter: not as the result of the function.

### RE: Accessing a DLL function

(OP)
Again the "Bad calling convention" error was raised.

Independent events are seldomly independent.

### RE: Accessing a DLL function

Have a look at the following
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

(OP)
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.

### RE: Accessing a DLL function

(OP)
Do you think VB6 will have the same variable issues as VBA?

Independent events are seldomly independent.

### RE: Accessing a DLL function

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: 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

(OP)
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.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!