473,654 Members | 3,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DllImport and structures containing char[]

I have a C++ dll which returns a structure, the structure
contains a char[21] variable, as shown below.

struct MyCStruct
{
short iNumber;
char Name[21];
};

I've declared a structure in C# as shown below

[StructureLayout[LayoutKind.Sequ ential)]
class MyCSharpStruct
{
short iNumber;
char[] Name;
}

when I call my dll function I pass the structure as a
LPStruct as shown below

[DllImport]
unsafe static extern bool MyDllFunction(s hort* iNumber,
[MarshalAs(Unman agedType.LPStru ct)]MyCSharpStruct
myStructure);

when I call this function (from an unsafe code block) I
get the error "Object reference is not set to an instance
of an object". What am I doing wrong? If I take the char
[21] element out of my C++ structure and my C# class and
call the dll everything works OK so I'm sure it the char
[] that causes the problem.
Nov 17 '05 #1
1 2558
LPStruct is one of the marshalling types (LPStruct is a
pointer to a C+ style structure). I think the problem is
due to initialising the char array but I've tried adding
this at several different points in the code and it still
gives an error. Also, why don't I have to initialise the
short?
-----Original Message-----
I don't have the ansewer but what is LPStruct
also do you have to init the char array?

[StructureLayout[LayoutKind.Sequ ential)]
class MyCSharpStruct
{
short iNumber;
char[21] Name; // init here?
}

"Stephen Richardson" <sr*********@ma zak.co.uk> wrote in messagenews:05******* *************** ******@phx.gbl. ..
I have a C++ dll which returns a structure, the structure contains a char[21] variable, as shown below.

struct MyCStruct
{
short iNumber;
char Name[21];
};

I've declared a structure in C# as shown below

[StructureLayout[LayoutKind.Sequ ential)]
class MyCSharpStruct
{
short iNumber;
char[] Name;
}

when I call my dll function I pass the structure as a
LPStruct as shown below

[DllImport]
unsafe static extern bool MyDllFunction(s hort* iNumber,
[MarshalAs(Unman agedType.LPStru ct)]MyCSharpStruct
myStructure);

when I call this function (from an unsafe code block) I
get the error "Object reference is not set to an instance of an object". What am I doing wrong? If I take the char
[21] element out of my C++ structure and my C# class and
call the dll everything works OK so I'm sure it the char
[] that causes the problem.

.

Nov 17 '05 #2

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

Similar topics

0
1502
by: Ivan | last post by:
Hi All, I have a problem with marshaling complex structures (containing numbers, strings, arrays of another structures) to native C function in dll. I have already posted same question to .compactframework thread, but nobody helped. main problem is that I have tried all the workarounds I have found in the Inet but nothing worked... I tried passing IntPtr, Byte Arrays, Serialisation, even used OpenNETCF, but
4
3854
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own malloc to do dynamic allocation from some static pool isn't really an option, for various reasons, not...
14
2990
by: tweak | last post by:
I'm struggling with the concept of typecasting when setting up a TCP client. Here's a code snip (modified from W. Richard Stevens Unix Programming book) to demonstrate where I am struggling: int sockfd; struct sockaddr_in servaddr; bzero(&servaddr, sizeof(servaddr));
5
2048
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After some investigating, I narrowed this down to a very odd behavior (bug?) of the VC++.NET 2003 compiler: If a class that is declared as __declspec(dllimport) derives from a template, that template's methods are never inlined, even if declared with...
1
3719
by: Thomas | last post by:
Hello all Having trouble with getting Error Information from spidermonkey. How would one convert a C structure to CSharp? struct JSErrorReport { const char *filename; // source file name, URL, etc., or null uintN lineno; // source line number const char *linebuf; // offending source line without final \n
13
3193
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the functions that require a structure to be passed to them. The function declaration in the .h file is of the form: SDCERR GetConfig(char *name, SDCConfig *cfg); where SDCConfig is a structure defined in the .h file. I am not much of a C (or C#)...
9
8204
by: jjones7947 | last post by:
Am doing a JNI wrap on a C++ API, am using VC7 and Eclipse. In preparation, I created a C++ executable which mimicked the flow of the JNI, i.e. a driver file which called methods in file with methods which call the API functions I want to use. That compiled and ran well. The C++ code is as follows: My header file void createSummarizer(char* rdir, char* lFile, char* key); void createInputOptions(const char* genre, const char* variant, const...
1
5792
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some code: #define USERINT_FUNC __cdecl #ifdef __cplusplus extern "C" { #endif
1
1712
by: mrssuper | last post by:
Hey All, How do I declare a following types: char** unsigned char** in C# DllImport function?
0
8290
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,...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5622
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
4149
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
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
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
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
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.