Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7704309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:39:18+00:00 2026-05-31T23:39:18+00:00

I’m currently writing a C dynamic library in MacOSX, the functions are then used

  • 0

I’m currently writing a C dynamic library in MacOSX, the functions are then used in Excel (2011) using the VBA, e.g.:

Declare Function TestF Lib "path_to_lib:mylib.dylib" Alias "test" (ByRef res As String) As String

This works fine for functions that return integers etc., but I can’t figure out how to pass strings back to VBA. My C function looks like this:

char* test(char *res)
{
  res = "test";
  return res;
}

but calling the function TestF in VBA as

Dim res As String
res = TestF(res)

crashes Excel…
If I use a function that is provided by the MathLink (Mathematica) library, e.g.

#include "mathlink.h"
char* test(MLINK link, char *res)
{
  MLGetString(link, &res);
  return res;
}

this function successfully assigns a string to “res” that I can use in VBA. The information on MLGetString can be found here: http://reference.wolfram.com/mathematica/ref/c/MLGetString.html.

So apparently, this function is able to create a string and pass it to res and I can use the string in VBA then. So any ideas how this function does that, or how I could pass a string to VBA without having to use OLE etc.?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:39:19+00:00Added an answer on May 31, 2026 at 11:39 pm

    Strings in Visual Basic are represented using BSTR objects: they consist of a pointer to a null-terminated wide-character string (UTF-16) which is preceded by a 4-byte length prefix. For example, here’s what the 4-character string “test” looks like in memory on a little-endian machine:

    +-------------+-------+-------+-------+-------+-------+
    | 08 00 00 00 | 74 00 | 65 00 | 73 00 | 74 00 | 00 00 |
    +-------------+-------+-------+-------+-------+-------+
     Length:        't'     'e'     's'     't'     '\0'
     8 bytes
     (not including null)
    

    Most importantly, the BSTR pointer itself points to the start of the string (the first 74 byte in this example), not the length prefix.

    For reading BSTRs, you can just treat them as regular pointers to wide-character strings, and you’ll be all right for the most part. But if you need to write/create them, it’s a little trickier.

    When creating BSTRs, you normally allocate them using SysAllocString (and its relatives) and deallocate them using SysFreeString. You need to figure out how Excel on OS X exposes those functions (likely through the OLE dynamic library), since they’re not standard C. If you try to return a string that wasn’t allocated through SysAllocString, then Excel is going to try to free it using SysFreeString, which will likely corrupt your heap and crash your program. So don’t do that.

    Also note that by default, a wchar_t has a size of 4 bytes, not 2, on OS X, so you can’t use the wchar_t data type when dealing with BSTRs—you need to use an explicit 16-bit data type (such as uint16_t), or compile with the -fshort-wchar compiler option to force wchar_t to be 2 bytes, but be aware of the binary incompatibility issues that that may cause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.