Joshua Sewell
Joshua Sewell

Reputation: 1

Need a solution for c++ into c#

Hope someone can point me in the right direction.

I started a windowsFormApplication in c# called WindowsFormApplication6 (I know, I'm super creative). Now I have a project in c++ ("Modbus Server") that I need to load into and run under project WindowsFormApplication6.

So I Googled it. What I found was that I need to load Modbus Server into WindowsFormApp via File, add existing project, and locate and click on the project.

Now upon more googling, I found that I need to add reference to Modbus Server.

So hightlight WindowsFormApp6, then right click it, hit add, then Reference, Solution, Projects, and finally click Modbus Server.

Lastly Google says that in Windows I need to write "using Modbus Server;" or "using WindowsFormApplication6.Modbus Server;" but every time I type "ModbusServer" it says "the type or Namespace Name 'ModbusServer' Could not be found" ("are you missing a using directive or an assembly reference?"). I have also split the word into "Modbus Server" which is what I would think, since that is how it's written everywhere else.

So now it looks like this "using Modbus Server;" but it keeps saying "Expected;"

I have looked over the modbus code, there is no namespace in it (c++) is it even possible to do what I want?

Upvotes: 0

Views: 69

Answers (1)

Frank Boyne
Frank Boyne

Reputation: 4570

Is your Modbus Server C++ project generating a managed assembly? If not, it may be that your Add Reference didn't actually do anything. In your C# project, if you expand the References node in Solution Explorer, do you see an entry for Modbus Server? Does that entry have a warning icon superimposed (a small yellow triangle with an exclamation mark)?

If your C++ project is generating native C++ I don't think Add Project Reference will work.

If Modbus Server is generating a COM object you could use Add Reference and the COM tab to add a reference to the COM object. But that means you'd have to register the COM object on the system you're doing your build on - either manually by running regsvr32 (or whatever is appropriate for your COM object) or automatically in the Modbus Server project.

If Modbus Server is a regular native C++ DLL (i.e., not a COM object) then you can't access it via Add Reference at all.

One alternative is P/Invoke which will let you describe the DLL entry points to C#.

Another alternative would be to write a C++/CLI wrapper that calls Modbus Server like any other C++ program would, but which declares .NET methods that can be called from C# to invoke the Modbus Server methods. Then you can use Add Reference to add the C++/CLI wrapper project to your C# project.

Upvotes: 1

Related Questions