1

I want to use C# to call c++ dll file to get data.

In C++,

extern "C" int _DLLExport Process_Read( char **msg );

In C#,

[DllImport( "ProcessExport" )]
public static extern int Process_Read( ref string msg );

=================

string msg;
int msg_len = 0;
msg_len = Process_Read( ref msg );

=================

How could I show the msg data? Thank you.

1
  • 4
    No sure what you mean by show data - have you tried Console.WriteLine(msg) after your call to Process_Read(..)? Commented Nov 25, 2013 at 6:46

1 Answer 1

2

DllImport can only be used to import C functions, not C++ class methods (even if they are static).

If its a c++ com object if you register it with regsvr32 you can add a reference to the dll in visual studio com references tab and usually visual studio creates an dll (I think its called a runtime callable wrapper) which you can see is created with the nameoflibrary.interop.dll. So MyExecRefsDll.dll if were a com object would become MyExexRefs.Interop.dll. But when you add the reference visual studio usually does this for you automatically in managed code. If you create a c++ dll as a com object by using atl template in c++ it is easier to access from dotnet (Iam referencing unmanaged c++ code from a dll which references another dll file (No code copied from the second dll I just reference the tlb, lib, the dll file, and visual studio does everything else.

Take a look at this tutorial it makes the process as clear as a crystal ball: http://www.codeproject.com/Articles/505791/A-Beginner-Tutorial-for-Writing-Simple-COM-ATL-DLL?q=creating+an+atl+dll+c%2b%2b

Just remember to find the tlb and lib (usually in the folder where the other dll is created after compiling. ) and reference them by right clicking in solution explorer and add->existing item for the second dll referenced from the main dll. The tlb and lib allow you to call functions from the dll (always copy new versions of them into your projects folder everytime you update the code to the second dll).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.