Solved

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

Posted on 2000-04-25
6
434 Views
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.
0
Comment
Question by:vasantha
6 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2750167
http://www.vb-helper.com/Howto/vcdll.zip
http://www.rdrop.com/~cary/program/mixed_language.html


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

0
 
LVL 32

Accepted Solution

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

http://support.microsoft.com/support/kb/articles/Q187/9/12.ASP
0
 
LVL 4

Expert Comment

by:CraigLazar
ID: 2752132
Hi
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);
        CoTaskMemFree(url);

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

hope it helps

craig
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:vasantha
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 ?
0
 
LVL 4

Expert Comment

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

:)

Craig
0
 

Author Comment

by:vasantha
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.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Paint/Redraw window while dragging 16 77
SQL VB connection works in one PC and doesn't in another 15 64
Visual Studio 2005 text editor 10 38
Spell Check in VB6 13 112
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

772 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