473,387 Members | 1,745 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,387 software developers and data experts.

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 * ufisConvertToBmp24(int handle);

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

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

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

For this I wrote

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

I think, this already is incorrect.

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

Thanks for any help of you specialists :-))

Greets

Tom
Nov 16 '05 #1
2 4044
Tom,

The declaration should look like this:

[DllImport ("ufisc.dll")]
private static extern IntPtr ufisConvertToBmp24(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 = ufisConvertToBmp24(<some value>);

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

Here is the problem. Without knowing how the memory was allocated from
within ufisConvertToBmp24, 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.com
"Tom" <ts@ck-de.net> wrote in message
news:2f**************************@posting.google.c om...
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 * ufisConvertToBmp24(int handle);

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

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

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

For this I wrote

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

I think, this already is incorrect.

How can I call the ufisConvertToBmp24 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.com>
wrote in
message news:<uI*************@TK2MSFTNGP14.phx.gbl>...
Tom,

The declaration should look like this:

[DllImport ("ufisc.dll")]
private static extern IntPtr ufisConvertToBmp24(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 = ufisConvertToBmp24(<some value>);

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

Here is the problem. Without knowing how the memory was allocated from
within ufisConvertToBmp24, 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.com

Nov 16 '05 #3

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

Similar topics

5
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 ...
6
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...
5
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...
1
by: b83503104 | last post by:
When are they not consistent?
22
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
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...
5
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
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.