PC Review


Reply
Thread Tools Rate Thread

calling a shared lib from VBA in Excel

 
 
Ian Kennedy
Guest
Posts: n/a
 
      23rd Feb 2007
Hi I'm attempting to call a function in a shared library from a VBA
macro in excel. This is a typical c function:

extern "C" bool SetBootstraps(int nBootstraps)

In VBA I have it declared as:

declare function SetBootstraps lib "mylib" (nBootstraps as long) as
Boolean

When I attempt to run this via the c debugger I don't get the value
passed down to excel that I would expect. What do I have to do to pass
variables down correctly. Do I need to use CDecl in the VBA or do I need
to use pascal in the C. Which is correct.

Also am I correct in thinking that the following is correct

VBA C
Integer short
Long int
Double double
Boolean bool

Thanks
Ian
 
Reply With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      23rd Feb 2007
Ian,

The C DLL function must have been compiled with the "__stdcall" modifier,
and the function must have been exported with a DEF file. Also, since the
SetBootstraps function requires a value of nBootstraps, not a pointer, you
need the ByVal modifier in your Declare statement.

Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long) As
Boolean


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)






"Ian Kennedy" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi I'm attempting to call a function in a shared library from a VBA macro
> in excel. This is a typical c function:
>
> extern "C" bool SetBootstraps(int nBootstraps)
>
> In VBA I have it declared as:
>
> declare function SetBootstraps lib "mylib" (nBootstraps as long) as
> Boolean
>
> When I attempt to run this via the c debugger I don't get the value passed
> down to excel that I would expect. What do I have to do to pass variables
> down correctly. Do I need to use CDecl in the VBA or do I need to use
> pascal in the C. Which is correct.
>
> Also am I correct in thinking that the following is correct
>
> VBA C
> Integer short
> Long int
> Double double
> Boolean bool
>
> Thanks
> Ian



 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      23rd Feb 2007

"Chip Pearson" <(E-Mail Removed)> wrote in message

> Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long) As
> Boolean


Should be

Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long) As
Long

Also, in VC++, there is "BOOL" and "bool", two distinct types: "BOOL" is a
long int (32-bits) and "bool" is a char (8-bits).

You can test the difference with

int __stdcall SizeOfBOOLUpperCase()
{
return sizeof(BOOL);
}

int _stdcall SizeOfboolLowerCase()
{
return sizeof(bool);
}

SizeOfBOOLUpperCase returns 4.
SizeOfboolLowerCase returns 1.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Chip Pearson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ian,
>
> The C DLL function must have been compiled with the "__stdcall" modifier,
> and the function must have been exported with a DEF file. Also, since the
> SetBootstraps function requires a value of nBootstraps, not a pointer, you
> need the ByVal modifier in your Declare statement.
>
> Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long) As
> Boolean
>
>
> --
> Cordially,
> Chip Pearson
> Microsoft MVP - Excel
> Pearson Software Consulting, LLC
> www.cpearson.com
> (email address is on the web site)
>
>
>
>
>
>
> "Ian Kennedy" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Hi I'm attempting to call a function in a shared library from a VBA macro
>> in excel. This is a typical c function:
>>
>> extern "C" bool SetBootstraps(int nBootstraps)
>>
>> In VBA I have it declared as:
>>
>> declare function SetBootstraps lib "mylib" (nBootstraps as long) as
>> Boolean
>>
>> When I attempt to run this via the c debugger I don't get the value
>> passed down to excel that I would expect. What do I have to do to pass
>> variables down correctly. Do I need to use CDecl in the VBA or do I need
>> to use pascal in the C. Which is correct.
>>
>> Also am I correct in thinking that the following is correct
>>
>> VBA C
>> Integer short
>> Long int
>> Double double
>> Boolean bool
>>
>> Thanks
>> Ian

>
>



 
Reply With Quote
 
Ian Kennedy
Guest
Posts: n/a
 
      23rd Feb 2007
Chip Pearson wrote:
> Ian,
>
> The C DLL function must have been compiled with the "__stdcall" modifier,
> and the function must have been exported with a DEF file. Also, since the
> SetBootstraps function requires a value of nBootstraps, not a pointer, you
> need the ByVal modifier in your Declare statement.


Thanks. I did have the ByVal in there I had forgotten to add it to by
post. The problem I have is that I am on a Mac.

What is the __stdcall equivilent on CodeWarrior, code warrior complains
at this structure. I have exported the function into my shared lib and
made it visible to the outside world. I can call into it but get junk
arguments passed down.

> Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long) As
> Boolean


Thanks
Ian
 
Reply With Quote
 
Ian Kennedy
Guest
Posts: n/a
 
      23rd Feb 2007
Chip Pearson wrote:
> "Chip Pearson" <(E-Mail Removed)> wrote in message
>
>> Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long) As
>> Boolean

>
> Should be
>
> Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long) As
> Long
>
> Also, in VC++, there is "BOOL" and "bool", two distinct types: "BOOL" is a
> long int (32-bits) and "bool" is a char (8-bits).


I'm not on windows, so BOOL is probably not available. What is Excel VBA
expecting for a boolean return value? It seems to be 16 bit so I could
just use a 'short'.

> You can test the difference with
>
> int __stdcall SizeOfBOOLUpperCase()
> {
> return sizeof(BOOL);
> }
>
> int _stdcall SizeOfboolLowerCase()
> {
> return sizeof(bool);
> }
>
> SizeOfBOOLUpperCase returns 4.
> SizeOfboolLowerCase returns 1.
>
>


Thanks again
Ian
 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      23rd Feb 2007
Ian,

The safest bet in to check with your compiler documentation to see exactly
how it defines "bool". In older version of MS VC, 'bool' was defined the
same size as 'int'. In version 4.2, it was changed to a single char.

Additionally, you could write a simple "Hello, World" program to display the
length of "bool" and use a compatible variable type in VBA. E.g,

long int SizeOfbool()
{
printf("Size Of 'bool' in bytes: %d", sizeof(bool));
return sizeof(bool);
}

If bool is a single byte, use the VBA Byte data type. E.g.,

Public Declare Function ReturnboolLC Lib "TestDLL.dll" () As Byte

Sub AAA()
Dim L As Long ' can be any integral variable type. VBA will convert.
L = ReturnboolLC()
Debug.Print CStr(L)
End Sub

The _stdcall calling convention is a Microsoft-specific option in C. It
isn't supported in ANSI C. Therefore, if you want to call functions in C DLL
from VB/VBA, you need to write non-ANSI C code. If you need to keep your
procedures ANSI-compatible, you'll need to write an additional DLL that
wraps your ANSI functions up in _stdcall functions and compile that DLL in
VC++. You can read more about __stdcall at

http://msdn.microsoft.com/library/de...___stdcall.asp



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"Ian Kennedy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Chip Pearson wrote:
>> "Chip Pearson" <(E-Mail Removed)> wrote in message
>>
>>> Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long)
>>> As Boolean

>>
>> Should be
>>
>> Declare Function SetBootstraps lib "mylib" (ByVal nBootstraps As Long) As
>> Long
>>
>> Also, in VC++, there is "BOOL" and "bool", two distinct types: "BOOL" is
>> a long int (32-bits) and "bool" is a char (8-bits).

>
> I'm not on windows, so BOOL is probably not available. What is Excel VBA
> expecting for a boolean return value? It seems to be 16 bit so I could
> just use a 'short'.
>
>> You can test the difference with
>>
>> int __stdcall SizeOfBOOLUpperCase()
>> {
>> return sizeof(BOOL);
>> }
>>
>> int _stdcall SizeOfboolLowerCase()
>> {
>> return sizeof(bool);
>> }
>>
>> SizeOfBOOLUpperCase returns 4.
>> SizeOfboolLowerCase returns 1.
>>
>>

>
> Thanks again
> Ian



 
Reply With Quote
 
Ian Kennedy
Guest
Posts: n/a
 
      23rd Feb 2007
Chip Pearson wrote:
> Ian,
>
> The safest bet in to check with your compiler documentation to see exactly
> how it defines "bool". In older version of MS VC, 'bool' was defined the
> same size as 'int'. In version 4.2, it was changed to a single char.
>
> Additionally, you could write a simple "Hello, World" program to display the
> length of "bool" and use a compatible variable type in VBA. E.g,
>
> long int SizeOfbool()
> {
> printf("Size Of 'bool' in bytes: %d", sizeof(bool));
> return sizeof(bool);
> }
>
> If bool is a single byte, use the VBA Byte data type. E.g.,
>
> Public Declare Function ReturnboolLC Lib "TestDLL.dll" () As Byte
>
> Sub AAA()
> Dim L As Long ' can be any integral variable type. VBA will convert.
> L = ReturnboolLC()
> Debug.Print CStr(L)
> End Sub


Sorry, you have be backwards, I was asking what Excel VBA used for the
size of Boolean, I will then match my C to that.

> The _stdcall calling convention is a Microsoft-specific option in C. It
> isn't supported in ANSI C. Therefore, if you want to call functions in C DLL
> from VB/VBA, you need to write non-ANSI C code. If you need to keep your
> procedures ANSI-compatible, you'll need to write an additional DLL that
> wraps your ANSI functions up in _stdcall functions and compile that DLL in
> VC++. You can read more about __stdcall at


Sorry, but I'm on a Macintosh, VC++ and DLLs have nothing to do with it.
Do you know what the calling convention should be for a Mac version of
Excel VBA? CodeWarrior on the Mac dose not have __stdcall so Excel can't
be using that, what does it use.
Thanks
Ian

> http://msdn.microsoft.com/library/de...___stdcall.asp
>
>
>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      23rd Feb 2007
"Ian Kennedy" <(E-Mail Removed)> wrote in message

> Sorry, you have be backwards, I was asking what Excel VBA used for the
> size of Boolean, I will then match my C to that.


I do have it backwards. Sorry about that. In VBA, a Boolean is a 2-byte
Integer.

> Sorry, but I'm on a Macintosh, VC++ and DLLs have nothing to do with it.


You should said that initially. It makes a big difference. I wouldn't have
wasted my time. Macs make up a very small part of the universe, so I assumed
you were on a Windows machine.

You can use

Sub AAA()
Dim B As Boolean
Debug.Print LenB(B)
End Sub

to see how many bytes are occupied by Boolean.

I have no idea how things work on Mac. You might ask in one the dedicated
Mac Office newsgroups.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"Ian Kennedy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Chip Pearson wrote:
>> Ian,
>>
>> The safest bet in to check with your compiler documentation to see
>> exactly how it defines "bool". In older version of MS VC, 'bool' was
>> defined the same size as 'int'. In version 4.2, it was changed to a
>> single char.
>>
>> Additionally, you could write a simple "Hello, World" program to display
>> the length of "bool" and use a compatible variable type in VBA. E.g,
>>
>> long int SizeOfbool()
>> {
>> printf("Size Of 'bool' in bytes: %d", sizeof(bool));
>> return sizeof(bool);
>> }
>>
>> If bool is a single byte, use the VBA Byte data type. E.g.,
>>
>> Public Declare Function ReturnboolLC Lib "TestDLL.dll" () As Byte
>>
>> Sub AAA()
>> Dim L As Long ' can be any integral variable type. VBA will convert.
>> L = ReturnboolLC()
>> Debug.Print CStr(L)
>> End Sub

>
> Sorry, you have be backwards, I was asking what Excel VBA used for the
> size of Boolean, I will then match my C to that.
>
>> The _stdcall calling convention is a Microsoft-specific option in C. It
>> isn't supported in ANSI C. Therefore, if you want to call functions in C
>> DLL from VB/VBA, you need to write non-ANSI C code. If you need to keep
>> your procedures ANSI-compatible, you'll need to write an additional DLL
>> that wraps your ANSI functions up in _stdcall functions and compile that
>> DLL in VC++. You can read more about __stdcall at

>
> Sorry, but I'm on a Macintosh, VC++ and DLLs have nothing to do with it.
> Do you know what the calling convention should be for a Mac version of
> Excel VBA? CodeWarrior on the Mac dose not have __stdcall so Excel can't
> be using that, what does it use.
> Thanks
> Ian
>
>> http://msdn.microsoft.com/library/de...___stdcall.asp
>>
>>


 
Reply With Quote
 
Ian Kennedy
Guest
Posts: n/a
 
      26th Feb 2007
Chip Pearson wrote:
> "Ian Kennedy" <(E-Mail Removed)> wrote in message
>
>> Sorry, you have be backwards, I was asking what Excel VBA used for the
>> size of Boolean, I will then match my C to that.

>
> I do have it backwards. Sorry about that. In VBA, a Boolean is a 2-byte
> Integer.
>
>> Sorry, but I'm on a Macintosh, VC++ and DLLs have nothing to do with it.

>
> You should said that initially. It makes a big difference. I wouldn't have
> wasted my time. Macs make up a very small part of the universe, so I assumed
> you were on a Windows machine.


Appologies, I thought I had but looking back I see that I didn't.

> You can use
>
> Sub AAA()
> Dim B As Boolean
> Debug.Print LenB(B)
> End Sub
>
> to see how many bytes are occupied by Boolean.
>
> I have no idea how things work on Mac. You might ask in one the dedicated
> Mac Office newsgroups.


I have but today I have had no response. The trouble with an Excel Mac
newsgroup is that this is really a programming question to do with
Excel. This would seem to be the correct group, perhaps it should be
renamed to include windows.

Thanks for you time
Ian
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling Shared Constructors/Finalizers Xenomech@gmail.com Microsoft VB .NET 2 22nd Jan 2007 04:08 PM
Calling to/from shared subs in vb.net .the.difference.engine. Microsoft ASP .NET 0 26th Oct 2005 02:16 PM
calling public shared function from a datagrid template column hansiman Microsoft ASP .NET 4 18th Jul 2005 03:52 PM
Calling Global shared sub not working tshad Microsoft ASP .NET 1 25th Feb 2005 04:37 AM
calling between C++ and Shared Libraries built with .NET nongo99 Microsoft Dot NET Framework 1 5th Aug 2003 06:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:23 PM.