473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using a DLL in C#

Hi All,

I have some source code writen in c that implements the blowfish encryption
algorithm which I have created as a DLL and want to use it in a .NET
solution.

I want to pass some params and get the encrypted string from it. I have
managed (as a test) to get my dll to return an integer and for the .NET to
accept it, however when I try to get the dll to return a char*, it fails due
to.....

ERROR{
An unhandled exception of type
'System.Runtime.InteropServices.MarshalDirectiveEx ception' occurred in
ConsoleApplication1.exe
Additional information: Can not marshal return value.
}

I have tried reading about managed and unmanaged code and about marshalling
but do not understand it enough to solve the problem.

The relevent code snips are below.

Can anyone help? :)

--Steve

C{
__declspec( dllexport ) char* BF_cbc_encrypt()

unsigned char *out;
........................
.......................
return out;
}

C#{
.............................
[DllImport("libeay32.dll")]
public static extern char[] BF_cbc_encrypt();
...............................
...............................
char[] ret = BF_cbc_encrypt();
System.Console.WriteLine(ret);
}

Nov 15 '05 #1
1 8102
Hi, Stephen
Normally you can use string to marshal char * return value. But maybe
what you want is a byte array not char array(unicode) reprentation, in such
case, you can use System.IntPtr to get the pointer, then use Marshal.Copy to
copy into a byte array. If you use Ptr, you can do more controls like
freeing the memory allocated by GlobalAlloc in native side.

[DllImport("libeay32.dll")]
public static extern string BF_cbc_encrypt();

or

[DllImport("libeay32.dll")]
public static extern System.IntPtr BF_cbc_encrypt();
Hope it helps!

Qiu


"Stephen Holly" <st***********@rockliffe.com> wrote in message
news:es**************@TK2MSFTNGP12.phx.gbl...
Hi All,

I have some source code writen in c that implements the blowfish encryption algorithm which I have created as a DLL and want to use it in a .NET
solution.

I want to pass some params and get the encrypted string from it. I have
managed (as a test) to get my dll to return an integer and for the .NET to
accept it, however when I try to get the dll to return a char*, it fails due to.....

ERROR{
An unhandled exception of type
'System.Runtime.InteropServices.MarshalDirectiveEx ception' occurred in
ConsoleApplication1.exe
Additional information: Can not marshal return value.
}

I have tried reading about managed and unmanaged code and about marshalling but do not understand it enough to solve the problem.

The relevent code snips are below.

Can anyone help? :)

--Steve

C{
__declspec( dllexport ) char* BF_cbc_encrypt()

unsigned char *out;
.......................
......................
return out;
}

C#{
............................
[DllImport("libeay32.dll")]
public static extern char[] BF_cbc_encrypt();
..............................
..............................
char[] ret = BF_cbc_encrypt();
System.Console.WriteLine(ret);
}

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
3
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
8
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
0
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
0
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.