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 / June 2005

Tip: Looking for answers? Try searching our database.

Please help me call this function from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Erik - 01 Jun 2005 12:42 GMT
Hi All,

I have a C++ DLL which basically calls a legacy C library which
decrypts some data.

I have defined the DLL as below.

extern "C" __declspec(dllexport) void  __stdcall init()
{
    init_decoder();
    return;
}

extern "C" __declspec(dllexport) short * __stdcall decode(unsigned
char serial[10])
{

    short * decoded_value = new short[80];
    decoder(serial, decoded_value); //This is where I call my legacy
library
   
    return decoded_value;
}

I define these in my C# code as

[DllImport("decoder.dll")]
private static extern char init();

[DllImport("decoder.dll")]
private static extern char decode(????);

public Test()
{
   init();
   byte [] DataToDecode = {57,100,182,27,75,192,103,99,56};
   byte [] DecodedData;
  //Want to pass the byte array to the decode function and have it
pass back the decoded data
  //Pseudo code
   DecodedData = decode(DataToDecode); ///???? How do I do this
}

I can call init() successfully so I know the DLL is working OK. I just
need to know how to call the decode part of it. What I need to do is
pass in a 10 character byte array and have the decoded data which
would be an array of shorts of length 80. If anybody can show me how
to do this I would be very grateful.

Many thanks,
Mattias Sjögren - 02 Jun 2005 13:44 GMT
>extern "C" __declspec(dllexport) short * __stdcall decode(unsigned
>char serial[10])
[quoted text clipped - 5 lines]
>    
>     return decoded_value;

I hope your library exposes a function to delete the decoded_value
buffer, or you'll leak memory.

>[DllImport("decoder.dll")]
>private static extern char init();

The return type should be void, just like in the native declaration.

>[DllImport("decoder.dll")]
>private static extern char decode(????);

Try

private static extern IntPtr decode(byte[] serial);

Use Marshal.Copy to dereference the returned pointer to a short[].

Mattias

Signature

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

Erik - 02 Jun 2005 19:32 GMT
Hi Mattias,

Thanks very much. Did everything you said and it all works perfectly!!

Many thanks

>>extern "C" __declspec(dllexport) short * __stdcall decode(unsigned
>>char serial[10])
[quoted text clipped - 24 lines]
>
>Mattias

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.