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

by msnews.microsoft.com » Tue, 11 Apr 2006 15:33:06 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
XXXX@XXXXX.COM



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

by Dmytro Lapshyn [MVP] » Tue, 11 Apr 2006 21:32:12 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)

Similar Threads

1. Passing in a string for a Bstr* IN parameter in a DLL using CFCOM

2. Callback from Unmanaged .dll using BSTR strings

I have a c++ .dll that uses a callback function pointer to a VB6 program. It 
passes BSTR strings to the VB6 callback function no problem. 

If I build a workalike callback function in VB.net 2005, the function works, 
but the strings have a length of 1 character, no matter what the lenght of 
the original BSTR string was. 

Using VB.net 2005, the strings contain only 1 character after the call back 
( this first letter of the bstr) .

This is true of (BSTR) strings as parameters or( BSTR) strings contained in 
structures, that are also BSTR types in the C++ dll structure that is passed 
to the VB.net called function. 

Any Ideas ?

3. passing query string as variable from aspx.cs to aspx - ASP.NET Web Controls

4. Problem passing string from c++ DLL to a c# main program - 64-bit

Is this the right forum for this kind of question?

I have a C++ DLL and a C# main program. The C++ program returns a C++ 
string:

      extern "C" __declspec(dllexport)  unsigned char * GetString()
      {
          return (unsigned char *)"A string";
      }

The C# program fetches the string:

        string ss = GetString();

When both main program and DLL are compiled 32-bit, this works.

When both main program and DLL are compiled 64-bit, I get:

     vshost.exe has stopped working


Jim

5. stl std::string passing to a dll - problem with debugger

6. String Truncation problem when marshalling LPTSTR string to a managed string

Hi Ying-Shen

I checked the sdk documentation for GetOpenFileName, and the following limit apply

---------------------------------------------------------------------------------------------------------------------------------------------
Note, when selecting multiple files, the total character limit for the file names depends on the operating system and the version of the function:

Windows 95/98/Me: (only ANSI is supported) no restriction
Microsoft Windows NT4 and earlier: 32k limit
Windows 2000/XP: (ANSI) 32k limit, (Unicode) no restriction

---------------------------------------------------------------------------------------------------------------------------------------------

In my application tried calling unicode ver of GetOpenFileNameW but still the string is truncated

We also wrote code to directly call GetOpenFileName from VC++ 6.0, there too the string is truncated

I am using Windows 2000 with Service Pack 4.  .Net framework Ver 1.1

I would appreciate any help

Thanks
Sandeep.

7. Must deep copy of BSTR to string ?

8. COM / SAFEARRAY(BSTR)* / String[] / System.Array

Hi

Here's a weird one which I would appreciate help with which I can't get my 
head around.....

If I create a .NET assembly that has a method in...

string[] GetFormats()

expose it as a COM components (via regasm), then create a new .NET app that 
references that COM component, my call to GetFormats() returns a String[] as 
expected. The IDL for the COM-wrapped .NET component is...

[id(0x60020001)]
HRESULT GetFormats( [out, retval] SAFEARRAY(BSTR)* pRetVal );

However....

If I create a pure COM component with the same method (GetFormats) that 
returns a SAFEARRAY of BSTR's, then reference it in my .NET app, the type 
returned from the GetFormats call is a System.Array!

The idl created for the COM is identical to the automatically generated idl 
for the wrapped .NET component. 

Can anyone give me any ideas as to why I can't make my 'pure' COM component 
method expose itself to .NET as a string[]?

Thanks

Jon