|
-
January 23rd, 2009, 05:06 AM
#1
Structures in C#
Hello Everyone,
I have made a DLL in VC++, in which one function requires a struct variable to be passed, and i want to use that DLL in my c# application. When i try to use that DLL in c# application, it is giving error as Bad Pointer...
Does anybody have solution to this....
Thanks in Advance,
Poornima .S
-
January 23rd, 2009, 06:25 AM
#2
Re: Structures in C#
either use C++/CLI to expose the C++ dll to C#, or p/invoke it and create structures in C# that are laid out the same as the structures in C++ and use that to pass data into the p/invoked function.
-
January 23rd, 2009, 07:12 AM
#3
Re: Structures in C#
 Originally Posted by MadHatter
either use C++/CLI to expose the C++ dll to C#, or p/invoke it and create structures in C# that are laid out the same as the structures in C++ and use that to pass data into the p/invoked function.
What do you mean by "use C++/CLI to expose the C++ dll to C#". Is not it the same thing the poster tried.
-
January 23rd, 2009, 09:19 AM
#4
Re: Structures in C#
no, you can create a .net assembly using C++ which is different than a native C++ dll which the OP has.
-
January 23rd, 2009, 09:31 AM
#5
Re: Structures in C#
 Originally Posted by MadHatter
no, you can create a .net assembly using C++ which is different than a native C++ dll which the OP has.
Now I get your point. You wanted to say Managed C++.
-
January 23rd, 2009, 09:43 AM
#6
-
January 23rd, 2009, 10:58 AM
#7
Re: Structures in C#
paste your code here did you used the extern when prototyping the function in c#?
Please rate my post if it was helpful for you.  Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
-
January 23rd, 2009, 12:48 PM
#8
Re: Structures in C#
If you want C# to talk to a native C++ dll then try using the interop assistant from Microsoft.
Darwen.
-
January 27th, 2009, 05:02 AM
#9
Re: Structures in C#
Hello Everyone thanks for your help..
This what my code looks like
//Structure declared in the VC++ DLL
struct EmpDetails
{
unsigned char * bNAME;
unsigned char * bAGE;
unsigned char * bDOJ;
unsigned char * bDESIGNATATION;
unsigned char * bGENDER;
unsigned char * bSALARY;
};
//A Function in the same DLL that takes the above declared structure as input
int __stdcall FnWriteEmpDetails(const EmpDetails &stEmpDtls, unsigned char * bReturnVal);
I have done a Wrapper class for that, the function declaration is as below.
[DllImport("TEST_DLL.dll")]
//int __stdcall FnWriteEmpDetails(const EmpDetails &stEmpDtls, unsigned char * bReturnVal);
public static extern int FnWriteEmpDetails(EmpDetails stEmpDtls, byte[] bReturnVal);
And my structure definition in Wrapper class in c# is :
public struct EmpDetails
{
public byte [] bNAME;
public byte [] bAGE;
public byte [] bDOJ;
public byte [] bDESIGNATATION;
public byte [] bGENDER;
public byte [] bSALARY;
};
When i try to use this function in C#, it is not taking the structure variable, my test app is giving the following error
An unhandled exception of type 'System.NullReferenceException' occurred in TEST.exe
Additional information: Object reference not set to an instance of an object.
Is there any solution for this?
Last edited by cheery_poori; January 27th, 2009 at 05:09 AM.
-
January 27th, 2009, 06:07 AM
#10
Re: Structures in C#
 Originally Posted by nabeelisnabeel
Now I get your point. You wanted to say Managed C++.
Technically speeking "Managed C++" is the language that was used with .NET 1.X and is no longer used.
The current language (.NET 2.0 thru 4.0) is C++/CLI.
The two are radically different. Originally Microsoft did not follow the "Extensibility guidelines" for C++ and created a very strange beast. C++/CLI on the other hand properly (well almost) follows the guidelines.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 27th, 2009, 06:13 AM
#11
Re: Structures in C#
C++/CLI is integration of native and managed programming.
Please rate my post if it was helpful for you.  Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
-
January 27th, 2009, 08:04 AM
#12
Re: Structures in C#
Your marshalling is incorrect.
I'm presuming that your structure is made up of null terminated C++ strings ? In which case your structure should look like :
Code:
[StructLayout(LayoutKind.Sequential)]
struct EmpDetails
{
[MarshalAs(UnmanagedType.LPStr)]
string bNAME;
[MarshalAs(UnmanagedType.LPStr)]
string bAGE;
[MarshalAs(UnmanagedType.LPStr)]
string bDOJ;
[MarshalAs(UnmanagedType.LPStr)]
string bDESIGNATATION;
[MarshalAs(UnmanagedType.LPStr)]
string bGENDER;
[MarshalAs(UnmanagedType.LPStr)]
string bSALARY;
};
The reason why you were getting a null-reference exeption in your case was you probably didn't assign anything to the byte [] arrays, so they were all null when passed into the method hence 'null exception'.
I could write a book on interop so I'm not going into details about this solution. Just remember you have to set each of the fields in the structure to a string before you pass it into your function.
Your function signature is also incorrect : but without knowing what 'bReturnVal' is (whether it is a single character to be output, or whether you have to pass an already allocated block of unsigned char memory to be filled by the function) I can't give you an interop signature for this.
i.e. in C++ whether you do
Code:
EmpDetails details;
char returnVal = 0;
FnWriteEmpDetails(stEmpDtls, &returnVal);
or
Code:
EmpDetails details;
std::vector<char> returnVal;
returnVal.resize(10); // or some size
FnWriteEmpDetails(stEmpDtls, &returnVal.front());
If it's a single character it would be :
Code:
[DllImport("TEST_DLL.dll")]
public static extern int FnWriteEmpDetails(ref EmpDetails stEmpDtls, [Out] out char bReturnVal);
If it's a character array it would be :
Code:
[DllImport("TEST_DLL.dll")]
public static extern int FnWriteEmpDetails(ref EmpDetails stEmpDtls,
[In][MarshalAs(UnmanagedType.LPArray)] byte [] bReturnVal);
// call like this :
EmpDetails stEmpDtls = new EmpDetails();
stEmpDtls.bNAME = "Name";
// fill in the other fields
byte [] buffer = new byte[10]; // or whatever size
FnWriteEmpDetails(ref EmpDetails, buffer);
Notice the 'ref' ? This is needed because the parameter stEmpDtls is being passed by reference.
As I said before for most cases you can either use the microsoft interop assistant of go to www.pinvoke.net for information.
Darwen.
Last edited by darwen; January 27th, 2009 at 08:17 AM.
-
January 28th, 2009, 01:47 AM
#13
Re: Structures in C#
Hello Darwen,
My Structure in C++ looks as below
struct EmpDetails
{
unsigned char * bNAME;
unsigned char * bAGE;
unsigned char * bDOJ;
unsigned char * bDESIGNATATION;
unsigned char * bGENDER;
unsigned char * bSALARY;
};
So in C# wrapper class, for unsigned char array, which type i should take for marshalling?
I tried taking different types and but it didn't work.
bReturnVal is an output parameter...there is no problem with that
-
January 28th, 2009, 05:35 AM
#14
Re: Structures in C#
I gave you the EmpDetails struct in C# in my last post......
Darwen.
-
January 28th, 2009, 06:23 AM
#15
Re: Structures in C#
Hello Darwen,
See if i want to give bDOJ as 22/01/2009, then in my dll function i want the day as 22, but if i pass as string in c# it won't take it as 22 instead it will take that as 2 so is there any other way to pass a byte value using marshalling.
Thanks & Regards,
Poornima.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|