473,783 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pinvoke, how to marshal "unsigned char *"

1 New Member
I'm trying to call some functions from OpenSSL library but I'm a bit confused when I have to use pinvoke.

first function should create key from some input data, it's declared :
Expand|Select|Wrap|Line Numbers
  1. void BF_set_key(BF_KEY *key, int len, const unsigned char *data);
I've translated it as
Expand|Select|Wrap|Line Numbers
  1. public static extern IntPtr BF_set_key (IntPtr key, int len, [In] byte[] data)
and I try to call it this way:
Expand|Select|Wrap|Line Numbers
  1.       byte[] data = 
  2.       { 
  3.         0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 
  4.         0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68 
  5.       };
  6. IntPtr key = Marshal.AllocHGlobal( 4168 );  // 4168 is size BF_KEY struct
  7. key = BlowFish . BF_set_key (key, data.Length, data);
next function should encrypt/decypt input block an write the result to ouput one, it is declared :
Expand|Select|Wrap|Line Numbers
  1. void BF_ecb_encrypt(const unsigned char *in, unsigned char *out, BF_KEY *key, int enc);
my managed wrapper:
Expand|Select|Wrap|Line Numbers
  1. public static extern void BF_ecb_encrypt
  2. ([In] byte[] input, [Out] byte[] output, IntPtr key, int enc);
calling it:
Expand|Select|Wrap|Line Numbers
  1. byte[] input = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68 };
  2. byte[] output = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  3. byte[] output2 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  4. BF_ecb_encrypt (input, output, key, 1); // encrypt
  5. BlowFish . BF_ecb_encrypt (output, output2, key, 0); // decrypt
I suppose that output2 will contains the same data as input after the last line executes but the results are strange - ecnryption returns some data but I'm afraid not the encrypted form of input, output2 returns some random data.

I'm pretty sure I do something wrong (probably several somethings:)) - please can you tell me, what ?
Apr 2 '07 #1
2 7317
rednael
1 New Member
Please read the following article:
http://blog.rednael.com/2008/08/29/M...DLLsInNET.aspx

It's an in depth article about marshalling between native code and managed code. It shows which types are interoperable, how to import a DLL, how to pass strings, how to pass structures and how to de-reference pointers.

Code examples in C# are included.
Sep 10 '08 #2
auralcircuitry
1 New Member
Was searching for exactly this, your link saved me! thank you
Oct 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

43
3425
by: M-One | last post by:
See subject: how do I calloc (and free the memory, if that's not free(my_bytes);) this? TIA!
4
10725
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain ASCII characters in the 0-127 range, although I had thought that I might want to use the 128-255 range for special symbols or foreign character codes. This all worked OK for a long time, but a recent update to the compiler on my system has...
1
9676
by: cbachellam | last post by:
Hello, I am trying to call c++ dll function calls from C#. The c++ dll function takes in pointer to unsigned char array. Can anybody help me in what marshalling parameter value that i have to declare in C# side. For eg: in c++ X.dll the function is: extern "C" __declspec(dllexport) void Getcharpointer(unsigned char* v) { //Just for test purpose, i alter and check on the C# side whether it
4
5073
by: Patrick | last post by:
Anyone have any ideas on how to Marshal this parameter in C#? unsigned char* buf TIA, Patrick
1
4779
by: jurot | last post by:
Hi. I have struct in C++: struct MY_STRUCT { int x; int y; char** arrNames; //array of strings }
7
16533
by: jeezamail | last post by:
I saw words like "marshal", "marshaler", "demarshaler" in some log files, articles but I really don't understand what it means. I have tried to google it but did not find any satisfied or clear answer. Can anyone give me some definitions, meanings or direct me to where I can find the answer. Thank you very much in advance. Jee
8
1793
by: FabioAng | last post by:
Assuming I have this function (it's not complete): template<typename InputType, typename OutputIterator> void to_utf8(InputType input, OutputIterator result) { // trivial conversion if (input < (1 << 7)) { *result = input; ++result; }else { *result = ((input >6) | 0xc0); ++result;
10
3129
by: Kenneth Brody | last post by:
Does the cast "(unsigned)" imply "(unsigned int)", or does it simply strip the signedness from the variable? In other words, given this: long l = -123; unsigned long ul = (unsigned)l; Does the value of "l" get demoted to an unsigned int during the assignment, or does it remain a long? (I am on a system where
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.