Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My unmanaged function signature is as follows:
HRESULT extern WINAPI WFSStartUp ( DWORD dwVersionsRequired, LPWFSVERSION lpWFSVersion);

Where LPWFSVERSION
I is a pointer to struct

I declared the following struct
[StructLayout(LayoutKind.Sequential)]
 public class WFSVERSION
 {
     public int            wVersion;
     public int            wLowVersion;
     public int            wHighVersion;
     public char[]           szDescription;
     public char[]           szSystemStatus;
 };

[DllImport(@"C:\WINDOWS\system32\msxfs.dll")]


[return: MarshalAs(UnmanagedType.I4)]
public static extern int WFSStartUp(double dwVersionsRequired, [MarshalAs(UnmanagedType.LPStruct)] ref WFSVERSION rc);

When calling the function in my C# application I get
SafeArrayTypeMismatchException

WFSVERSION rc = new WFSVERSION();


rc.szDescription = new char[256];
rc.szSystemStatus = new char[256];

int result = WFSStartUp(130819, ref rc);


i understand that the struct needs to be of IDispatch interface type so I attempted

to do the following:
WFSVERSION rc = new WFSVERSION();
DispatchWrapper dispArray = new DispatchWrapper((object)rc);

But then it gives me a compile error saying that my cast is incorrect. Please advise. I ran out of ideas.
Posted
Updated 6-Dec-11 13:35pm
v3

Yes, your error is in
C++
WFSVERSION rc = new WFSVERSION();//incorrect
WFSVERSION rc();//correct
WFSVERSION *rc = new WFSVERSION(); //correct
 
Share this answer
 
are you getting your languages mixed up?

This is valid in C# but invalid in C/C++

WFSVERSION rc = new WFSVERSION();
DispatchWrapper dispArray = new DispatchWrapper((object)rc); 


This is valid in C/C++ nut invalid in C#

WFSVERSION *rc = new WFSVERSION();
DispatchWrapper *dispArray = new DispatchWrapper((object)rc); 


So you need to make sure you match the syntax to the piece of code you are using in the interop pieces.
 
Share this answer
 
I've implementated the following C# statements as you pointed out:
WFSVERSION rc = new WFSVERSION();
DispatchWrapper dispArray = new DispatchWrapper((object)rc);

it builds but when calling the function in my C# application I get
SafeArrayTypeMismatchException
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900