473,748 Members | 8,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving an unsigned char pointer out of C++ DLL in CSharp ?

Tom
Hi newsgroup,

I have read a lot af articles about marshalling in C#, but none of
them could help me to solve the following problem:

There is a C-DLL with the following header-file:

++++++++ C Header file start +++++++++++++++ +++++++++++++++
#ifndef __UFISC_H
#define __UFISC_H
#ifdef UFISC_EXPORTS
#define UFIS __declspec( dllexport )
#else
#ifdef UFISC_IMPORTS
#define UFIS __declspec( dllimport )
#else
#define UFIS
#endif
#endif

//Some more code originated here
#pragma pack(push, 1)
#ifdef __cplusplus
extern "C"
{
#endif

//some more code originated here

extern unsigned char UFIS * ufisConvertToBm p24(int handle);

#ifdef __cplusplus
}
#endif
#pragma pack(pop)

#endif
++++++++ C Header file end +++++++++++++++ +++++++++++++++

I need to call the function ufisConvertToBm p24 out of my C# program.

For this I wrote

[DllImport ("ufisc.dll" )]
private static extern char [] ufisConvertToBm p24(int handle);

I think, this already is incorrect.

How can I call the ufisConvertToBm p24 function and retrieve the
correct value in my C# prog ?

Thanks for any help of you specialists :-))

Greets

Tom
Nov 16 '05 #1
2 4067
Tom,

The declaration should look like this:

[DllImport ("ufisc.dll" )]
private static extern IntPtr ufisConvertToBm p24(int handle);

The reason you do this is that it is passing back a pointer to memory,
and you need to marshal that back to a string, with a call like this:

// Call the API.
IntPtr retVal = ufisConvertToBm p24(<some value>);

// Create a string.
string val = Marshal.PtrToSt ringAnsi(retVal );

Here is the problem. Without knowing how the memory was allocated from
within ufisConvertToBm p24, using this method will result in a memory leak.
You should find out how to deallocate the memory, and then call that
afterwards as well (you will have to pass the pointer back to another
function).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Tom" <ts@ck-de.net> wrote in message
news:2f******** *************** ***@posting.goo gle.com...
Hi newsgroup,

I have read a lot af articles about marshalling in C#, but none of
them could help me to solve the following problem:

There is a C-DLL with the following header-file:

++++++++ C Header file start +++++++++++++++ +++++++++++++++
#ifndef __UFISC_H
#define __UFISC_H
#ifdef UFISC_EXPORTS
#define UFIS __declspec( dllexport )
#else
#ifdef UFISC_IMPORTS
#define UFIS __declspec( dllimport )
#else
#define UFIS
#endif
#endif

//Some more code originated here
#pragma pack(push, 1)
#ifdef __cplusplus
extern "C"
{
#endif

//some more code originated here

extern unsigned char UFIS * ufisConvertToBm p24(int handle);

#ifdef __cplusplus
}
#endif
#pragma pack(pop)

#endif
++++++++ C Header file end +++++++++++++++ +++++++++++++++

I need to call the function ufisConvertToBm p24 out of my C# program.

For this I wrote

[DllImport ("ufisc.dll" )]
private static extern char [] ufisConvertToBm p24(int handle);

I think, this already is incorrect.

How can I call the ufisConvertToBm p24 function and retrieve the
correct value in my C# prog ?

Thanks for any help of you specialists :-))

Greets

Tom

Nov 16 '05 #2
Tom
Nicholas,

that worked just fine.

The pointer holds bytedata for a Bitmap. I've handed the IntPtr retVal
directly to System.Drawings .Bitmap-constructor and the result was
perfect :-))

Thanks for your competent help !

Tom

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om>
wrote in
message news:<uI******* ******@TK2MSFTN GP14.phx.gbl>.. .
Tom,

The declaration should look like this:

[DllImport ("ufisc.dll" )]
private static extern IntPtr ufisConvertToBm p24(int handle);

The reason you do this is that it is passing back a pointer to memory,
and you need to marshal that back to a string, with a call like this:

// Call the API.
IntPtr retVal = ufisConvertToBm p24(<some value>);

// Create a string.
string val = Marshal.PtrToSt ringAnsi(retVal );

Here is the problem. Without knowing how the memory was allocated from
within ufisConvertToBm p24, using this method will result in a memory leak.
You should find out how to deallocate the memory, and then call that
afterwards as well (you will have to pass the pointer back to another
function).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

Nov 16 '05 #3

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

Similar topics

5
8984
by: wallacej | last post by:
Does anybody know why unsigned char myImage; works but unsigned char myImage; does not? I think its because the second line is a value too big for unsigned char, is this correct? If so:
6
14121
by: Sona | last post by:
Hi, What is the difference between a signed 0x00 (NULL, or 0) and an unsigned 0x00? Can there be one? If I do the following: char var; var = 0x00; what should var hold? and if it was an unsigned char, what should it hold then? Thanks
5
17649
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
1
2833
by: b83503104 | last post by:
When are they not consistent?
22
5617
by: juanitofoo | last post by:
Hello, I've just switched to gcc 4 and I came across a bunch of warnings that I can't fix. Example: #include <stdio.h> int main() { signed char *p = "Hola";
4
14975
by: cdrom205 | last post by:
static void MDString ( unsigned char *input) { MD5_CTX context; unsigned char digest; unsigned int len = sizeof(input);//strlen (const char*) md5.MD5Init (&context); md5.MD5Update (&context, input, len); // void MD5Update (MD5_CTX *context, unsigned char *input, unsigned
5
2849
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
30
3278
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
12
1792
by: Seth | last post by:
I have a unsigned char *data that points to a 32 character string. I need to retrieve 2 characters in the middle of the string. What is the best way to do this? Platform is embedded 8-bit atmel processor. Thanks
0
8991
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
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8244
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...
0
6076
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
4607
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
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.