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

Pointer to structure

Hi.

I have a problem with sending a struct from managed C# into an unmanaged C
code. I'm getting nullreference exception.
Tried below solution with ref also.

struct in c looks like this:

typedef struct
{
int width;
int height;
int x_adv;
int y_adv;
int bpl;
} iTypeWrapper_Glyph;

function decl in c looks like this:

extern "C" __declspec(dllexport) int iTypeWrapper_GetGlyph(long lUCode,
iTypeWrapper_Glyph* glyph, iTypeWrapper_GlyphMapFmt gmf);

struct in c# looks like this:

[StructLayout (LayoutKind.Sequential)]
internal struct iTypeWrapper_Glyph
{
public int width;
public int height;
public int x_adv;
public int y_adv;
public int bpl;
};

PInvoke decl in C# looks like this:

[DllImport("test.dll", SetLastError=true,CharSet = CharSet.Ansi)]
internal static extern iTypeWrapper_Glyph iTypeWrapper_GetGlyph(long
lUCode, [MarshalAs(UnmanagedType.LPStruct)] iTypeWrapper_Glyph oGlyph
,iTypeWrapper_GlyphMapFmt gmf);

The function call in C# looks like this:

iTypeWrapper_Glyph oGlyphWrapper = new iTypeWrapper_Glyph();

oGlyphWrapper = RasterizerHandler.iTypeWrapper_GetGlyph(80, oGlyphWrapper,
iTypeWrapper_GlyphMapFmt.ITYPE_WRAPPER_BITMAP_DATA );

I want to send a struct pointer into the C function so it can fill the
struct and then I want to use it in C#.

Please help me. :-) Been sitting with this now for one whole day.

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #1
2 8296
Andreas,
[DllImport("test.dll", SetLastError=true,CharSet = CharSet.Ansi)]
internal static extern iTypeWrapper_Glyph iTypeWrapper_GetGlyph(long
lUCode, [MarshalAs(UnmanagedType.LPStruct)] iTypeWrapper_Glyph oGlyph
,iTypeWrapper_GlyphMapFmt gmf);


Try changing it to

[DllImport("test.dll", SetLastError=true,CharSet = CharSet.Ansi)]
internal static extern int iTypeWrapper_GetGlyph(int lUCode, ref
iTypeWrapper_Glyph oGlyph, iTypeWrapper_GlyphMapFmt gmf);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hi.

Thanks for your fast reply.

I thought I tried this once before, but when I pasted in your code and
recompiled then it worked.

Then I noticed you changed from long to int on the lUCode.
?Seemed like it did not like the long?

Thanks a lot.

Thank god it's friday.

Kind regards Andreas

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #3

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

Similar topics

20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
6
by: Fernan Bolando | last post by:
What is the best way of passing a structure to a function currently I do by it passing a pointer to the structe. sample code below int main() { struct sample_struct a_struct, *point_struct;...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
19
by: junky_fellow | last post by:
Can the size of pointer variables of different type may be different on a particular architecture. For eg. Can the sizeof (char *) be different from sizeof(int *) or sizeof (void *) ? What...
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
0
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,...
0
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...
0
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...

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.