How to call a Win32 dll function with BSTR parameter from VB

Posted on 2000-04-25
Medium Priority
Last Modified: 2008-03-10
How do I call a function residing in a Win32 dll(developed using VC++) which takes a BSTR as a parameter ?
The function prototype in VC++ is as follows
extern "C"
PRESENTATION_API void __stdcall Sample(BSTR sName);

I tried calling the function in VB using the following declaration.
Private Declare Sub Sample Lib "presentation" Alias "_Sample@4" (ByVal sName As String)

I didnot succeed in my attempt. However, if I change my function parameter in VC++ to a variant, I could successfully call the same from VB with a small modification to the function declaration.
Question by:vasantha
LVL 28

Expert Comment

ID: 2750167

two things you might want to check out regarding creating vc++ dlls for use with vb

LVL 32

Accepted Solution

Erick37 earned 150 total points
ID: 2751435
"HOWTO: Pass a String Between Visual Basic and Your C DLL"


Expert Comment

ID: 2752132
in c use the bstring function like this

#include "comutil.h"
    BSTR mess;
 mess = _com_util::ConvertStringToBSTR("Step - This is also funky!");
        IDoug->DougThis (&mess);
        CoTaskMemFree (mess);
        url = _com_util::ConvertStringToBSTR ("http://www.microsoft.com");
        IDoug->gothere (&url);

this works when passing a c string to my vb activex dll or exe

hope it helps

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 2753912
As mentioned in the article 12.ASP(thanks to Eric37), a BSTR string will be automatically be converted to plain ASCII while passing the same from VB to a C dll. Does it mean that I will not be able to accept a string from VB as a BSTR in a C dll ?

Expert Comment

ID: 2755313
as far as i know u can go from vb to c using the opposite function



Author Comment

ID: 2791140
From the article that Erick has suggested, I understand that a BSTR string from VB is automatically converted to ASCII string when sent to a C dll. So, I have modified my VC dll function to accept a LPSTR.

Thanks Erick for the suggestion.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question