Pointer to structure

  • Thread starter Andreas Kristiansson via DotNetMonster.com
  • Start date
A

Andreas Kristiansson via DotNetMonster.com

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.
 
M

Mattias Sjögren

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
 
A

Andreas Kristiansson via DotNetMonster.com

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top