By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

Pointer to structure

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.