Using C++ DLL in C# (cant view methods or structs)

  • Thread starter Thread starter Mas L via DotNetMonster.com
  • Start date Start date
M

Mas L via DotNetMonster.com

Hi,

I have a c++ source code which I can compile to be a DLL (in VS.NET 2003).
And I need to use it in a C# program.

After I compiled/build the C++ code to a DLL, I add it as a Reference in my
C# program.

When I look at the reference and I double click to view it in the objec
browser , I see only structs , without their members. And I don't see methods.


Is there a way to compile the DLL so I can at least have the structs with
their members. The methods I can DLLImport but the structs not and they are
very important.

Can somebody please help? Thanks a lot in advance.

NETFAN
 
Hi,

For importing structs, declare the struct
[StructLayout(LayoutKind.Sequential)]
public struct SampleStruct
{
public uint field1;
public uint field2;
}

Declare API function that uses the struct as follows

[DllImport("YourModule.DLL")]
static extern void StructUsage(SampleStruct st);

Hope that helps.

Regards
Vivek Ragunathan
 
Big Thx but... :)

Is that possible if the structs use types that are not available in C#?

something like this...

struct loService
{
loDriver driver;
unsigned long initphase;
loCaller cactx;

unsigned cform_dataonly,
cform_datatime,
cform_writecompl;

loClient *servlist;
int shutdown; /* indicates the shutdown condition */
unsigned serv_key; /* generator for server's IDs */
lw_mutex lkList; /* servlist & shutdown */
lw_rwlock lkMgmt; /* adding & searching in tags[] */
/* using of allocated tags is unlocked */
/* changing of lastused - through lkSec */
lw_rwlock lkPrim; /* access to tags::primXXX */
lw_mutex lkDr; /* serialise ldSubscribe()
ldWriteTags(), ldReadTags() */
loThrControl update_pipe;
lw_condb lkTridWait;
/* lock secondary, and lasused & other counts,
.state = 1 when secondary updated */
int (*wstrcmp)(const loWchar *, const loWchar *);
int (*wstrncmp)(const loWchar *, const loWchar *, unsigned);
int (*wstrhash)(const loWchar *);
#if 0
int (*wstrnhash)(const loWchar *, unsigned);
#endif

loWchar branch_sep;
unsigned tag_count; /* valid ti is: 0 < ti < tag_count */
unsigned firstfree; /* free ti is >= firstfree */
unsigned lastused; /* valid ti is <= lastused */
unsigned lastnamed; /* valid named ti is <= lastnamed */
loTagEntry *tags;
loTagValue *secondary;
lo_hash *name_hash;
loTrid sec_trid, prim_trid;
#if LO_EV_TIMESTAMP
FILETIME *ts_prim, *ts_sec;
unsigned ts_size;
#endif
/* loTrid prim_changed;*/

struct
loProperty **proplist;
unsigned proplist_count;

void *log;

loService *iam;
};

Hi,

For importing structs, declare the struct
[StructLayout(LayoutKind.Sequential)]
public struct SampleStruct
{
public uint field1;
public uint field2;
}

Declare API function that uses the struct as follows

[DllImport("YourModule.DLL")]
static extern void StructUsage(SampleStruct st);

Hope that helps.

Regards
Vivek Ragunathan
 
Mas L via DotNetMonster.com said:
Hi,

I have a c++ source code which I can compile to be a DLL (in VS.NET 2003).
And I need to use it in a C# program.

After I compiled/build the C++ code to a DLL, I add it as a Reference in
my
C# program.

When I look at the reference and I double click to view it in the objec
browser , I see only structs , without their members. And I don't see
methods.


Is there a way to compile the DLL so I can at least have the structs with
their members. The methods I can DLLImport but the structs not and they
are
very important.

Can somebody please help? Thanks a lot in advance.

NETFAN

To use DllImport (PInvoke interop), the functions to be called from C# must
be exported as native C style function. Native code DLL's can't be
referenced in VS unless they are registerd COM servers, so I wonder how you
managed to set a reference to such DLL.

Willy.
 
Thx

Well I did, in the Build options I set "Managed Extensions" to "Yes"
And I could reference it. Ok I can see that there is prob only one way to
solve this and translate all my c/c++ code to C# ?? Cuz I need the structs
and their specific types.

Dllimport is not enough.

If I do it with dllimport and no reference , I have no structs and I need
them.
And I cant import the structs cuz he doesnt recognize halve of types in that
struct.

What can I do?

Kind Regards

[quoted text clipped - 17 lines]

To use DllImport (PInvoke interop), the functions to be called from C# must
be exported as native C style function. Native code DLL's can't be
referenced in VS unless they are registerd COM servers, so I wonder how you
managed to set a reference to such DLL.

Willy.
 
I see, you compiled your C++ code as a managed DLL, but this is not enough
for it to be usable from C#. The reason for this is that while compiling
with managed "extensions enabled" your code translates to MSIL, your classes
remain unmanaged and cannot be used directly from C#.
To solve this issue you have basically two options:
- Convert all of your native C++ classes to managed classes.
- Write a managed wrapper for your unmanaged implementation, that way you
can keep the unmanaged DLL as is, while from managed code you call the
unmanaged implementation through a "managed proxy" interface that delegates
the calls to unmanaged code.

C# ----> Managed proxy ----> Unmanaged class(es).
managed C++ unmanaged C++

Willy.


Mas L via DotNetMonster.com said:
Thx

Well I did, in the Build options I set "Managed Extensions" to "Yes"
And I could reference it. Ok I can see that there is prob only one way to
solve this and translate all my c/c++ code to C# ?? Cuz I need the structs
and their specific types.

Dllimport is not enough.

If I do it with dllimport and no reference , I have no structs and I need
them.
And I cant import the structs cuz he doesnt recognize halve of types in
that
struct.

What can I do?

Kind Regards

[quoted text clipped - 17 lines]

To use DllImport (PInvoke interop), the functions to be called from C#
must
be exported as native C style function. Native code DLL's can't be
referenced in VS unless they are registerd COM servers, so I wonder how
you
managed to set a reference to such DLL.

Willy.
 
How do I do option 1?

Thx in advance
I see, you compiled your C++ code as a managed DLL, but this is not enough
for it to be usable from C#. The reason for this is that while compiling
with managed "extensions enabled" your code translates to MSIL, your classes
remain unmanaged and cannot be used directly from C#.
To solve this issue you have basically two options:
- Convert all of your native C++ classes to managed classes.
- Write a managed wrapper for your unmanaged implementation, that way you
can keep the unmanaged DLL as is, while from managed code you call the
unmanaged implementation through a "managed proxy" interface that delegates
the calls to unmanaged code.

C# ----> Managed proxy ----> Unmanaged class(es).
managed C++ unmanaged C++

Willy.
[quoted text clipped - 29 lines]
 
Check these:
http://msdn.microsoft.com/library/d...pec/html/vcManExMigrationGuidePart1_Start.asp

http://msdn.microsoft.com/library/d...pec/html/vcManExMigrationGuidePart1_Start.asp

they have answers to all of your questions.

Willy.

Mas L via DotNetMonster.com said:
How do I do option 1?

Thx in advance
I see, you compiled your C++ code as a managed DLL, but this is not enough
for it to be usable from C#. The reason for this is that while compiling
with managed "extensions enabled" your code translates to MSIL, your
classes
remain unmanaged and cannot be used directly from C#.
To solve this issue you have basically two options:
- Convert all of your native C++ classes to managed classes.
- Write a managed wrapper for your unmanaged implementation, that way you
can keep the unmanaged DLL as is, while from managed code you call the
unmanaged implementation through a "managed proxy" interface that
delegates
the calls to unmanaged code.

C# ----> Managed proxy ----> Unmanaged class(es).
managed C++ unmanaged C++

Willy.
[quoted text clipped - 29 lines]
 

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

Back
Top