Exporting Managed Code as Flat API to Unmanaged World

S

Saad

Hi,

I have a MC++dll, whose functions i want to use in unmanaged c++. I
read that one of the ways to use the managed functions in unmanaged
world is by exposing the managed api as flat api.
I tried that and with simple data types it worked. But now i need to
export a function which needs to take a gc struct as parameter, and
then fill some values in it, and then the unmanaged c++ will use that
info.

Here's what i did

//My gc struct
[StructLayout(LayoutKind::Sequential,CharSet=CharSet::Ansi)]
public __gc struct STLayout {
public:
[MarshalAs(UnmanagedType::LPTStr)]
String* ID;
};
//End

Now i'm trying to export this through a funtion like this:-

__declspec(dllexport) int
ExportL([MarshalAs(UnmanagedType::Struct)]STLayout*& Layout)

And when i compile this dll. i'm getting the following error:-

error C3395: __declspec(dllexport) cannot be applied to a function
with the __clrcall calling convention

Now im stuck....
what i dont understand is that

1) why isn't the MarshalAs attribute working here.....
2) What is the preferred way of exporting the managed types to c+
+....other than the COM interop, mixed-mode solutions......

Any other ideas...

Thanks in advance...
Saad
 
M

Massood

Hi,

I have a MC++dll, whose functions i want to use in unmanaged c++. I
read that one of the ways to use the managed functions in unmanaged
world is by exposing the managed api as flat api.
I tried that and with simple data types it worked. But now i need to
export a function which needs to take a gc struct as parameter, and
then fill some values in it, and then the unmanaged c++ will use that
info.

Here's what i did

//My gc struct
[StructLayout(LayoutKind::Sequential,CharSet=CharSet::Ansi)]
public __gc struct STLayout {
public:
[MarshalAs(UnmanagedType::LPTStr)]
String* ID;};

//End

Now i'm trying to export this through a funtion like this:-

__declspec(dllexport) int
ExportL([MarshalAs(UnmanagedType::Struct)]STLayout*& Layout)

And when i compile this dll. i'm getting the following error:-

error C3395: __declspec(dllexport) cannot be applied to a function
with the __clrcall calling convention

Now im stuck....
what i dont understand is that

1) why isn't the MarshalAs attribute working here.....
2) What is the preferred way of exporting the managed types to c+
+....other than the COM interop, mixed-mode solutions......

Any other ideas...

Thanks in advance...
Saad

As I know, functions with managed types cannot be exported as flat
API. Maybe I'm wrong(??).
But you can pass the struct members separately as simple data type
variables to the function, or you can try an unmanaged struct
corresponding the managed one, and then convert it to a managed one
inside the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top