How do I call C++ functions from a C# coded Service?

C

Chip Gore

Hi -

Using Visual Studio 2005, I have created a stand alone command
line .exe program that "does what I want".

All of the functionality that I would like to make available to my
C#/.NET service is in a few .cpp/.h files (that my .exe main line .cpp
file calls and uses fine).

I'm not sure what I need to do to make my C++ code into something that
can be called from the C# code. I've seen people talk about "P/
Invoke", but all of the documentation that I've found assumes I
have .dll, and I'm not sure what I need to do to change my C++ project
from creating .exe into creating a .dll so I can pick up where the "P/
Invoke" documentation seems to be starting from.

(I know, just a bit "Newby" of me, but that's where I'm at)

I gather that once I have a .dll, I can then import this, and make a
declaration within my C# code to say "this lives over there" and the
rest of the process should flow naturally. (or do I have my hopes set
to high for this?)

Thanks in advance
cg
 
S

shashank kadge

In VS2005,
1. create new project, select VC++, select Windows service, so u can
have all the .header file's code into this windows service project.
Build this project.
2. Create new project, select C#, select windows console. Call the
window service created in step 1 from this project.

-
shashank kadge
 
B

Ben Voigt [C++ MVP]

Chip said:
Hi -

Using Visual Studio 2005, I have created a stand alone command
line .exe program that "does what I want".

All of the functionality that I would like to make available to my
C#/.NET service is in a few .cpp/.h files (that my .exe main line .cpp
file calls and uses fine).

I'm not sure what I need to do to make my C++ code into something that
can be called from the C# code. I've seen people talk about "P/
Invoke", but all of the documentation that I've found assumes I
have .dll, and I'm not sure what I need to do to change my C++ project
from creating .exe into creating a .dll so I can pick up where the "P/
Invoke" documentation seems to be starting from.

You probably don't want p/invoke. C++ interop is much easier to use and
more efficient.

The big question -- is that same C++ code used by other programs?
If no -- convert it to C++/CLI, by changing "class whatever" to "ref class
whatever", using System::String instead of std::string, and so on.
If yes -- write a ref class wrapper that converts System::String objects
into std::string objects, then calls the existing code.
(I know, just a bit "Newby" of me, but that's where I'm at)

I gather that once I have a .dll, I can then import this, and make a
declaration within my C# code to say "this lives over there" and the
rest of the process should flow naturally. (or do I have my hopes set
to high for this?)

By using C++/CLI, you will get a true .NET assembly that can be added to
your C# code (whether class library, console application, service, or
whatever) as a reference just like you do with System.Drawing, System.Data,
System.Xml, etc.
 

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