runtime error coming while using MarshalAs attribute

From managed class, I am making a call to a function which is inside a native DLL and has signature like this : Inside C++ file :
int FWCreateStore(char *path, PCSStore *ppStore, CSPDB **ppDB)

where : # define CSPStore *PCSStore
and CSPStore and CSPDB are two classes whose data members are another class pointers or simply ints or chars.
This is there inside the DLL "FWrap"

Now , come to C# file from where i am calling this function . Here I have included it like this :

[DllImport("FWrap")]
[return:MarshalAs(UnmanagedType.U4) ]
public static extern int FWCreateStore(
[MarshalAs(UnmanagedType.LPStr)] string path,
[MarshalAs(UnmanagedType.LPStruct)] out IntPtr ipStore,
[MarshalAs(UnmanagedType.LPStruct)] out IntPtr ipDB);

internal class F4
{
IntPtr pStore;
static IntPtr pDB;
string DbPath;

internal FlaimError.Error CreateStore()
{
FError.Error rc = FError.Error.FERR_FAILURE;
lock (handleTable)
{

rc = (FError.Error) FWCreateStore(DbPath, out pStore, out pDB);
}
}
}

-- This code compiles . When i run my application then I am getting this exception :

System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #3': Invalid managed/unmanaged type combination (Int/UInt must be paired with SysInt or SysUInt).
at FWCreateStore(String path, IntPtr& ipStore, IntPtr& ipDB)

Can anyone tell me , what wrong i am doing with 3rd parameter passing.
Or if you can write a sample code , how to pass object pointers pointing to class . I tried taking help from MSDN links about MarshalAs attribute but they have not written any example (about passing object pointers) .
Any light in the tunnel . pl
thanks in advance ,
Anil.

[1834 byte] By [Anil15] at [2008-1-6]
# 1

Hi Anil15,

If you are passing a plain C++ class with only plain old data, and without any virtual function or virtual base class, it is safe to pass it using P/Invoke, because the behavior is just like normal structs in C. Otherwise, a wrapper must be written in C++/CLI, or you can convert the C++ class to COM, and just pass the interface.

For class nested with pointer to another Class, the same rule applies. Also you need to add special marshalling directives to tell CLR to marshal the pointer to the class as well.

Could you please show the managed defination of the two classes: CSPStore and CSPDB?

And I'd like to suggest that you'd better using C++/CLI to write a warapper for complex C++ classes and functions.

Thanks!

FengChen-MSFT at 2007-9-28 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 2

Hi Anil15,

We are marking this issue as "Answered". If you have any new findings or concerns, please feel free to unmark the issue.

Thanks for your understanding!

FengChen-MSFT at 2007-9-28 > top of Msdn Tech,.NET Development,Common Language Runtime...

.NET Development

Site Classified