Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Using "wsprintf".

Good day, all.

I have just been trying to use the very useful wsprintf API function in VB5,
and every time I use it, I get "Bad DLL Calling Convention". This message
generally means that I have done the declare incorrectly, e.g.:

Private Declare Function wsprintf Lib "user32" Alias "wsprintfA" ( _
ByVal lpOut As String, _
ByVal lpFmt As String, _
ByVal s1 As String, _
ByVal s2 As String, _
ByVal s3 As String, _
ByVal s4 As String, _
ByVal s5 As String, _
ByVal s6 As String, _
ByVal s7 As String, _
ByVal s8 As String, _
ByVal s9 As String, _
ByVal s10 As String) As Long

This is probably happening because parameters s1 to s10 are optional. At
a guess, VB is a lot more stringent about what you can throw on the stack
than C, which language the related printf and sprintf functions come from.

I remember somebody (was it Damit?) was talking about an alternative way
to call API functions. Would this alternate mechanism do what I want. If
so, how is it done?

Thanks in advance,

--------
Mark Alexander Bertenshaw
Programmer/Analyst
Prime Response
Brentford
UK
[1210 byte] By [Mark Alexander Bertenshaw] at [2007-11-10 0:20:11]
# 1 Re: Using "wsprintf".
Mark,

>I have just been trying to use the very useful wsprintf API function in VB5,
>and every time I use it, I get "Bad DLL Calling Convention". This message
>generally means that I have done the declare incorrectly, e.g.:

No, it means that that function uses the _cdecl calling convention,
which makes it impossible to call directly from VB.

There is however an alternative function, wvsprintf, that works the
same, but uses the _stdcall convention that VB likes. Here's a
Deja.com link to a message posted by Brad Martinez a while ago in the
MS newsgroups that has some sample code:

http://x42.deja.com/[ST_rn=ps]/getdoc.xp?AN=588198258&CONTEXT=953303627.1739522051&hitnum=1

And the MSDN docs are located at

http://msdn.microsoft.com/library/psdk/winui/strings_92hy.htm

Mattias

__________________________________________________
Mattias Sjgren (MCP) - mattiass @ hem.passagen.se
VB+ http://hem.spray.se/mattias.sjogren/
Please send questions/replies to the newsgroups
Mattias Sjgren at 2007-11-11 20:04:07 >
# 2 Re: Using "wsprintf".
Mark,

>I have just been trying to use the very useful wsprintf API function in VB5,
>and every time I use it, I get "Bad DLL Calling Convention". This message
>generally means that I have done the declare incorrectly, e.g.:

No, it means that that function uses the _cdecl calling convention,
which makes it impossible to call directly from VB.

There is however an alternative function, wvsprintf, that works the
same, but uses the _stdcall convention that VB likes. Here's a
Deja.com link to a message posted by Brad Martinez a while ago in the
MS newsgroups that has some sample code:

http://x42.deja.com/[ST_rn=ps]/getdoc.xp?AN=588198258&CONTEXT=953303627.1739522051&hitnum=1

And the MSDN docs are located at

http://msdn.microsoft.com/library/psdk/winui/strings_92hy.htm

Mattias

__________________________________________________
Mattias Sjgren (MCP) - mattiass @ hem.passagen.se
VB+ http://hem.spray.se/mattias.sjogren/
Please send questions/replies to the newsgroups
Mattias Sjgren at 2007-11-11 20:05:06 >
# 3 Re: Using "wsprintf".
Mattias -

Thanks very much. Am I right in assuming that va_list is an array of string
pointers?

--

------------
Mark Alexander Bertenshaw
Programmer/Analyst
Prime Response
Brentford
UK
"Mattias Sjgren" <mattiass.dont.want.spam@hem.passagen.se> wrote in message
news:38d243d5.285594082@news.dev-archive.com...
> Mark,
>
> >I have just been trying to use the very useful wsprintf API function in
VB5,
> >and every time I use it, I get "Bad DLL Calling Convention". This message
> >generally means that I have done the declare incorrectly, e.g.:
>
> No, it means that that function uses the _cdecl calling convention,
> which makes it impossible to call directly from VB.
>
> There is however an alternative function, wvsprintf, that works the
> same, but uses the _stdcall convention that VB likes. Here's a
> Deja.com link to a message posted by Brad Martinez a while ago in the
> MS newsgroups that has some sample code:
>
>
http://x42.deja.com/[ST_rn=ps]/getdoc.xp?AN=588198258&CONTEXT=953303627.1739
522051&hitnum=1
>
> And the MSDN docs are located at
>
> http://msdn.microsoft.com/library/psdk/winui/strings_92hy.htm
>
>
> Mattias
>
> __________________________________________________
> Mattias Sjgren (MCP) - mattiass @ hem.passagen.se
> VB+ http://hem.spray.se/mattias.sjogren/
> Please send questions/replies to the newsgroups
Mark Alexander Bertenshaw at 2007-11-11 20:06:10 >
# 4 Re: Using "wsprintf".
Mattias -

Thanks very much. Am I right in assuming that va_list is an array of string
pointers?

--

------------
Mark Alexander Bertenshaw
Programmer/Analyst
Prime Response
Brentford
UK
"Mattias Sjgren" <mattiass.dont.want.spam@hem.passagen.se> wrote in message
news:38d243d5.285594082@news.dev-archive.com...
> Mark,
>
> >I have just been trying to use the very useful wsprintf API function in
VB5,
> >and every time I use it, I get "Bad DLL Calling Convention". This message
> >generally means that I have done the declare incorrectly, e.g.:
>
> No, it means that that function uses the _cdecl calling convention,
> which makes it impossible to call directly from VB.
>
> There is however an alternative function, wvsprintf, that works the
> same, but uses the _stdcall convention that VB likes. Here's a
> Deja.com link to a message posted by Brad Martinez a while ago in the
> MS newsgroups that has some sample code:
>
>
http://x42.deja.com/[ST_rn=ps]/getdoc.xp?AN=588198258&CONTEXT=953303627.1739
522051&hitnum=1
>
> And the MSDN docs are located at
>
> http://msdn.microsoft.com/library/psdk/winui/strings_92hy.htm
>
>
> Mattias
>
> __________________________________________________
> Mattias Sjgren (MCP) - mattiass @ hem.passagen.se
> VB+ http://hem.spray.se/mattias.sjogren/
> Please send questions/replies to the newsgroups
Mark Alexander Bertenshaw at 2007-11-11 20:07:09 >
# 5 Re: Using "wsprintf".
I get "Bad DLL Calling Convention". This message
>>generally means that I have done the declare incorrectly, e.g.:

Before VB calls a function, it grabs a copy of the stack pointer. When the
function returns, VB compares the stack pointer to the copy. If they are
different, VB restores the correct value and raises this error. So if the
length of all the parameters combined is incorrect, you will get this error.

Mike
Mike at 2007-11-11 20:08:14 >
# 6 Re: Using "wsprintf".
I get "Bad DLL Calling Convention". This message
>>generally means that I have done the declare incorrectly, e.g.:

Before VB calls a function, it grabs a copy of the stack pointer. When the
function returns, VB compares the stack pointer to the copy. If they are
different, VB restores the correct value and raises this error. So if the
length of all the parameters combined is incorrect, you will get this error.

Mike
Mike at 2007-11-11 20:09:18 >
# 7 Re: Using "wsprintf".
Mike -

Thanks for the explanation. I presume that this is a "safety" feature which
C, due to its lower level language status doesn't require, thus allowing you
a lot more flexibility as how you can pass arguments. However, it appears
that VB <can> do this by using "optional" parameters.

--

------------
Mark Alexander Bertenshaw
Programmer/Analyst
Prime Response
Brentford
UK
"Mike" <m_culley@one.net.au> wrote in message news:38d38c21@news.dev-archive.com...
>
> I get "Bad DLL Calling Convention". This message
> >>generally means that I have done the declare incorrectly, e.g.:
>
> Before VB calls a function, it grabs a copy of the stack pointer. When the
> function returns, VB compares the stack pointer to the copy. If they are
> different, VB restores the correct value and raises this error. So if the
> length of all the parameters combined is incorrect, you will get this
error.
>
> Mike
Mark Alexander Bertenshaw at 2007-11-11 20:10:16 >
# 8 Re: Using "wsprintf".
Mike -

Thanks for the explanation. I presume that this is a "safety" feature which
C, due to its lower level language status doesn't require, thus allowing you
a lot more flexibility as how you can pass arguments. However, it appears
that VB <can> do this by using "optional" parameters.

--

------------
Mark Alexander Bertenshaw
Programmer/Analyst
Prime Response
Brentford
UK
"Mike" <m_culley@one.net.au> wrote in message news:38d38c21@news.dev-archive.com...
>
> I get "Bad DLL Calling Convention". This message
> >>generally means that I have done the declare incorrectly, e.g.:
>
> Before VB calls a function, it grabs a copy of the stack pointer. When the
> function returns, VB compares the stack pointer to the copy. If they are
> different, VB restores the correct value and raises this error. So if the
> length of all the parameters combined is incorrect, you will get this
error.
>
> Mike
Mark Alexander Bertenshaw at 2007-11-11 20:11:13 >
# 9 Re: Using "wsprintf".
Mark,

This is a safety feature. I believe that VB puts the parameters on the stack
and the function in the DLL takes them off. So if VB puts on 8 bytes, but
the DLL takes off 4 bytes... I'm sure you get the picture :) So instead of
a GPF, you get a runtime error. Quite a nice feature. But in C you would
get a crash

I'm guessing here, but I presume that VB adds in optional parameters in the
background, so they are still there as far as the called function is concerned.

Mike

"Mark Alexander Bertenshaw" <Mark.Bertenshaw@virgin.net> wrote:
>Mike -
>
>Thanks for the explanation. I presume that this is a "safety" feature which
>C, due to its lower level language status doesn't require, thus allowing
you
>a lot more flexibility as how you can pass arguments. However, it appears
>that VB <can> do this by using "optional" parameters.
>
>--
>
>------------
>Mark Alexander Bertenshaw
>Programmer/Analyst
>Prime Response
>Brentford
>UK
>"Mike" <m_culley@one.net.au> wrote in message news:38d38c21@news.dev-archive.com...
>>
>> I get "Bad DLL Calling Convention". This message
>> >>generally means that I have done the declare incorrectly, e.g.:
>>
>> Before VB calls a function, it grabs a copy of the stack pointer. When
the
>> function returns, VB compares the stack pointer to the copy. If they are
>> different, VB restores the correct value and raises this error. So if
the
>> length of all the parameters combined is incorrect, you will get this
>error.
>>
>> Mike
>
>
Mike at 2007-11-11 20:12:21 >
# 10 Re: Using "wsprintf".
Mark,

This is a safety feature. I believe that VB puts the parameters on the stack
and the function in the DLL takes them off. So if VB puts on 8 bytes, but
the DLL takes off 4 bytes... I'm sure you get the picture :) So instead of
a GPF, you get a runtime error. Quite a nice feature. But in C you would
get a crash

I'm guessing here, but I presume that VB adds in optional parameters in the
background, so they are still there as far as the called function is concerned.

Mike

"Mark Alexander Bertenshaw" <Mark.Bertenshaw@virgin.net> wrote:
>Mike -
>
>Thanks for the explanation. I presume that this is a "safety" feature which
>C, due to its lower level language status doesn't require, thus allowing
you
>a lot more flexibility as how you can pass arguments. However, it appears
>that VB <can> do this by using "optional" parameters.
>
>--
>
>------------
>Mark Alexander Bertenshaw
>Programmer/Analyst
>Prime Response
>Brentford
>UK
>"Mike" <m_culley@one.net.au> wrote in message news:38d38c21@news.dev-archive.com...
>>
>> I get "Bad DLL Calling Convention". This message
>> >>generally means that I have done the declare incorrectly, e.g.:
>>
>> Before VB calls a function, it grabs a copy of the stack pointer. When
the
>> function returns, VB compares the stack pointer to the copy. If they are
>> different, VB restores the correct value and raises this error. So if
the
>> length of all the parameters combined is incorrect, you will get this
>error.
>>
>> Mike
>
>
Mike at 2007-11-11 20:13:15 >
# 11 Re: Using "wsprintf".
"Mike" <m_culley@one.net.au> wrote:
>
>I get "Bad DLL Calling Convention". This message
>>>generally means that I have done the declare incorrectly, e.g.:
>
>Before VB calls a function, it grabs a copy of the stack pointer. When the
>function returns, VB compares the stack pointer to the copy. If they are
>different, VB restores the correct value and raises this error. So if the
>length of all the parameters combined is incorrect, you will get this error.
>
>Mike

Note from MSDN on wsprintf:
"Note Unlike other Windows functions, wsprintf uses the C calling convention
(_cdecl), rather than the Pascal calling convention. As a result, it is the
responsibility of the calling process to pop arguments off the stack, and
arguments are pushed on the stack from right to left. In C-language modules,
the C compiler performs this task. "

If a function is compiled with _cdecl, it is just not possible to call from
VB, because vb used _stdcall calling convention.
and hence you *WILL* get Bad Dll calling convention.
Ravi Kiran at 2007-11-11 20:14:20 >
# 12 Re: Using "wsprintf".
"Mike" <m_culley@one.net.au> wrote:
>
>I get "Bad DLL Calling Convention". This message
>>>generally means that I have done the declare incorrectly, e.g.:
>
>Before VB calls a function, it grabs a copy of the stack pointer. When the
>function returns, VB compares the stack pointer to the copy. If they are
>different, VB restores the correct value and raises this error. So if the
>length of all the parameters combined is incorrect, you will get this error.
>
>Mike

Note from MSDN on wsprintf:
"Note Unlike other Windows functions, wsprintf uses the C calling convention
(_cdecl), rather than the Pascal calling convention. As a result, it is the
responsibility of the calling process to pop arguments off the stack, and
arguments are pushed on the stack from right to left. In C-language modules,
the C compiler performs this task. "

If a function is compiled with _cdecl, it is just not possible to call from
VB, because vb used _stdcall calling convention.
and hence you *WILL* get Bad Dll calling convention.
Ravi Kiran at 2007-11-11 20:15:18 >