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 / November 2003

Tip: Looking for answers? Try searching our database.

Returning string from DLL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hakon - 25 Nov 2003 10:10 GMT
Hello,

I need to call a function in a legacy, unmanaged DLL that
has the following syntax:
int GetVersion(char **version);

The 'version' parameter needs to be a pre-allocated string
of size 250 and it will be filled by the function. The
function both zero-terminate the version string as well as
return the length of the version string.

I have tried several approaches - all with exceptions or
no data returned. Can anyone help me out with a way to
call this function from C#?

I think the behaviour is related to some legacy VB6
integration....

Thanks in advance,
Hakon
Jens Thiel - 25 Nov 2003 10:57 GMT
> I need to call a function in a legacy, unmanaged DLL that
> has the following syntax:
[quoted text clipped - 4 lines]
> function both zero-terminate the version string as well as
> return the length of the version string.

I would try something like

   [ DllImport("Unmanaged.dll", CharSet=CharSet.Ansi, ExactSpelling=true) ]
   private static extern void GetVersion(StringBuilder buf);

and pass a StringBuilder with a capacity of at least 250 characters.

Jens.

Signature

http://ManagedXLL.net/  |  http://jens-thiel.de/  |  http://QuantLib.net/
Replace MSDN with my first name when replying to my email address!

- 25 Nov 2003 11:26 GMT
Well, I have been down that road and it does not work :(

A snip of my C# code:
[DllImport("MyGetVersion.dll", CharSet=CharSet.Ansi,
ExactSpelling=true)]
private static extern int MyGetVersion(StringBuilder
version);

private static void Test()
{
    StringBuilder version = new StringBuilder(250);
    MyGetVersion(version);
    System.Diagnostics.Debug.WriteLine(version);
}

I have created my own unmanaged DLL to try a few things
out and the MyGetVersion function looks like this:

extern "C" MYGETVERSION_API int MyGetVersion(char
**version)
{
    char *target = *version;
    strncpy(target, "Version 1.1", 249);
    return (int) strlen(target);
}

Calling the C++ from C# gives me an access denied error...

Thanks,
Hakon

>-----Original Message-----
>> I need to call a function in a legacy, unmanaged DLL that
[quoted text clipped - 14 lines]
>
>Jens.
Mattias Sj?gren - 25 Nov 2003 12:10 GMT
Hakon,

>A snip of my C# code:
>[DllImport("MyGetVersion.dll", CharSet=CharSet.Ansi,
>ExactSpelling=true)]
>private static extern int MyGetVersion(StringBuilder
>version);

Make the StringBuilder a ref parameter to add the extra level of
indirection needed.

private static extern int MyGetVersion(ref StringBuilder version);

Mattias

Signature

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

- 25 Nov 2003 12:45 GMT
Hehe - I was sure that I tried that before but now it
works :D

Thanks Mattias and Jens

/H

>-----Original Message-----
>Hakon,
[quoted text clipped - 11 lines]
>
>Mattias

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.