+ Reply to Thread
Results 1 to 7 of 7

How to return a string of unknown size from DLL to Excel Visual Basic in C ?

  1. #1
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    How to return a string of unknown size from DLL to Excel Visual Basic in C ?

    Dear All,

    Now I'm trying to receive the string return value from DLL to VBA.
    "C-DLL code "
    Please Login or Register  to view this content.

    and the below is the VBA code

    Please Login or Register  to view this content.
    when I ran above code, I've got receive well the d1 and d2 data but the problem is that "s" I can't get it.

    Is it possible to directly return the resulting string from the DLL? Or is there any other way to dynamically allocate a string object depending on the length of the result and return it to a VB caller?

    I tried different approaches, for example like this (ugly, just examples), but I can't get exactly the string value what I want.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: How to return a string of unknown size from DLL to Excel Visual Basic in C ?

    Did you try
    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to return a string of unknown size from DLL to Excel Visual Basic in C ?

    Can you use a BSTR in your C code?
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to return a string of unknown size from DLL to Excel Visual Basic in C ?

    Hello u24c02,

    I could be wrong on this but I think your string needs to be called ByVal and not ByRef. The reason is when a variable name is declared as a String type in VBA, the variable returns the address of the declared string's name and not the first byte of the string itself as it does in "C". If you pass the variable ByRef (by reference) you will pass the address of the VBA declared string's name. If you pass it ByVal (by value) then you pass the contents of the decalred string variable which is the address of the string itself.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    Re: How to return a string of unknown size from DLL to Excel Visual Basic in C ?

    Quote Originally Posted by xlnitwit View Post
    Can you use a BSTR in your C code?
    As I know the BSTR can't use in the C.

  6. #6
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    Re: How to return a string of unknown size from DLL to Excel Visual Basic in C ?

    Quote Originally Posted by Leith Ross View Post
    Hello u24c02,

    I could be wrong on this but I think your string needs to be called ByVal and not ByRef. The reason is when a variable name is declared as a String type in VBA, the variable returns the address of the declared string's name and not the first byte of the string itself as it does in "C". If you pass the variable ByRef (by reference) you will pass the address of the VBA declared string's name. If you pass it ByVal (by value) then you pass the contents of the decalred string variable which is the address of the string itself.
    Thanks for your reply,
    Yes you are right. I'm supposed to have a ByVal.
    ThAnks
    Last edited by u24c02; 03-14-2017 at 11:46 PM.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to return a string of unknown size from DLL to Excel Visual Basic in C ?

    Hello u24c02,

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?
    By u24c02 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-13-2017, 09:25 PM
  2. Visual Basic in EXCEL
    By Raury in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2013, 11:07 AM
  3. [SOLVED] Can I run Visual Basic procedure using Excel Visual Basic editor?
    By john.jacobs71 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-26-2005, 10:25 AM
  4. [SOLVED] Excel in Visual Basic
    By Spike in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-29-2005, 02:15 PM
  5. Replies: 1
    Last Post: 09-13-2005, 07:06 AM
  6. [SOLVED] Excel and Visual Basic
    By \remy khalil via OfficeKB.com\ in forum Excel General
    Replies: 1
    Last Post: 07-07-2005, 09:05 AM
  7. [SOLVED] visual basic in excel -
    By DKehl in forum Excel General
    Replies: 2
    Last Post: 03-23-2005, 04:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1