Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2006

Tip: Looking for answers? Try searching our database.

Problem passing string as BSTR string to C DLL - Nullreference exception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
msnews.microsoft.com - 11 Apr 2006 08:33 GMT
Platform: VB.Net 1.1

I am trying to pass a string in VB.Net to a C Dll method which accepts a
BSTR. I get a nullreference exception whenever I call the API.

The following VBA declaration works fine in my previous Application:
Dim ddl As String
...
set_DatafilePath (ddl)

...
Declare Sub set_DatafilePath Lib "mydll.dll" (ByVal datafilepath As String)

In VB.Net, I have tried following:
String path = "somepath"
set_DatafilePath (path)

a) Declare Sub set_DatafilePath Lib "mydll.dll" ( ByVal datafilepath As
String)
b) Declare Sub set_DatafilePath Lib "mydll.dll" (
<MarshalAs(UnmanagedType.BStr)> ByVal datafilepath As String)
c) <DllImport("mydll.dll", CharSet:=CharSet.Auto)> _ 'Have tried ANSI as
well as Unicode as well
Public Shared Sub set_DatafilePath(<MarshalAs(UnmanagedType.BStr)> ByVal
datafilepath As String)
End Sub
d) <DllImport("mydll.dll", CharSet:=CharSet.Auto)> _
Public Shared Sub set_DatafilePath( ByVal datafilepath As String)
End Sub

Any help would be appreciated.

Cheers,

Manish Jain
manish04@hotmail.com
Dmytro Lapshyn [MVP] - 11 Apr 2006 14:32 GMT
Hi!

Here's how Microsoft recommends such functions to be declared (taken from
MSDN):

Public Declare Auto Sub PassBStr Lib "StringLib.Dll" _
(<MarshalAs(UnmanagedType.BStr)> s As String)

> Platform: VB.Net 1.1
>
[quoted text clipped - 33 lines]
> Manish Jain
> manish04@hotmail.com

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.