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 / December 2004

Tip: Looking for answers? Try searching our database.

newbie how to call this C function from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ted Sung - 03 Dec 2004 18:59 GMT
Hi,

I have a C function that's declared like this:

int my_function( BSTR *input, BSTR *output ) ;

Here BSTR is the windows BSTR type.
The output argument is allocated within my_function.

I declared it in my C# class as follows:

[DllImport("mydll.dll")]
public static extern int my_function( StringBuilder in, StringBuilder
out);

and when I run my program, I get the exception:

Object reference not set to an instance of an object:

My class does the following:

StringBuilder in, out;
in = new StringBuilder(100);
out = new StringBuilder(100);

int retval = my_function( in, out );

First, what does this error message mean?
Second, are my argument types wrong?  Since my_function() allocates
the value to be returned via the out argument, perhaps something else
has to be done.

Thanks,

Ted
Mattias Sj?gren - 03 Dec 2004 21:16 GMT
>[DllImport("mydll.dll")]
>public static extern int my_function( StringBuilder in, StringBuilder
>out);

I don't see how that could even compile, considering that out is a
keyword.

But try it like this

[DllImport("mydll.dll")]
public static extern int my_function([MarshalAs(UnmanagedType.BStr)]
ref string input, [MarshalAs(UnmanagedType.BStr)] out string output);

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

teds@intex.com - 06 Dec 2004 13:53 GMT
Fantastic, that worked.  Thank you very much.

Could I bother you for an explanation as to why this declaration works?
I thought that if strings were going to be allocated within
the my_function() I would need to use StringBuilder.

Ted

Mattias Sjögren wrote:
> >[DllImport("mydll.dll")]
> >public static extern int my_function( StringBuilder in, StringBuilder
[quoted text clipped - 15 lines]
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
Nick - 06 Dec 2004 17:01 GMT
Hi Ted,

The reason Mattias' example works is because he is telling the CLR to create
pointers to old style un-managed strings. You were asking it to pass in a
stringbuilder object to the dll function by value.

Your dll function will only accept unmanaged strings, of type BStr which is
why he is using Marshalling for the string object, so that the CLR can
recognise this.

Hope this makes sense.

Nick

Fantastic, that worked.  Thank you very much.

Could I bother you for an explanation as to why this declaration works?
I thought that if strings were going to be allocated within
the my_function() I would need to use StringBuilder.

Ted

Mattias Sj?gren wrote:
> >[DllImport("mydll.dll")]
> >public static extern int my_function( StringBuilder in,
StringBuilder
> >out);
>
[quoted text clipped - 13 lines]
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
teds@intex.com - 07 Dec 2004 14:59 GMT
[DllImport("mydll.dll")]
public static extern int my_function([MarshalAs(UnmanagedType.BStr)]
ref string input, [MarshalAs(UnmanagedType.BStr)] out string output);

So by making the parameters ref string and out string, this actually
makes them pointers to the variable ?  The out is used only to indicate
that the direction of the data flow ?  Finally, how do you decide to
use the string type versus the StringBuilder type?

Thanks again.

Ted

> Hi Ted,
>
[quoted text clipped - 44 lines]
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.799 / Virus Database: 543 - Release Date: 19/11/2004
teds@intex.com - 08 Dec 2004 17:33 GMT
Finally, when I try to do a

Console.Writeln( output ) ;

I get garbage.  Do I need to marshal this value to get it to print and
to be accessible to my other code?

Thanks,

Ted

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.