Using "wsprintf".


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Using "wsprintf".

  1. #1
    Mark Alexander Bertenshaw Guest

    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

  2. #2
    Mattias Sjögren Guest

    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/ps...rings_92hy.htm


    Mattias

    __________________________________________________
    Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    VB+ http://hem.spray.se/mattias.sjogren/
    Please send questions/replies to the newsgroups

  3. #3
    Mattias Sjögren Guest

    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/ps...rings_92hy.htm


    Mattias

    __________________________________________________
    Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    VB+ http://hem.spray.se/mattias.sjogren/
    Please send questions/replies to the newsgroups

  4. #4
    Mark Alexander Bertenshaw Guest

    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 Sjögren" <mattiass.dont.want.spam@hem.passagen.se> wrote in message
    news:38d243d5.285594082@news.devx.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/ps...rings_92hy.htm
    >
    >
    > Mattias
    >
    > __________________________________________________
    > Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    > VB+ http://hem.spray.se/mattias.sjogren/
    > Please send questions/replies to the newsgroups




  5. #5
    Mark Alexander Bertenshaw Guest

    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 Sjögren" <mattiass.dont.want.spam@hem.passagen.se> wrote in message
    news:38d243d5.285594082@news.devx.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/ps...rings_92hy.htm
    >
    >
    > Mattias
    >
    > __________________________________________________
    > Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    > VB+ http://hem.spray.se/mattias.sjogren/
    > Please send questions/replies to the newsgroups




  6. #6
    Mike Guest

    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

  7. #7
    Mike Guest

    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

  8. #8
    Mark Alexander Bertenshaw Guest

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




  9. #9
    Mark Alexander Bertenshaw Guest

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




  10. #10
    Mike Guest

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

    >
    >



  11. #11
    Mike Guest

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

    >
    >



  12. #12
    Ravi Kiran Guest

    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.


  13. #13
    Ravi Kiran Guest

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center