Re: Access C++ dll from C#
C# does not support C++ ABI (which very few, if any languages other than C++ does). The only real option is to write like a C++/ CLI wrapper. What this does is to create .NET classes that wrap around all the native functions. Be aware, though there are some things you should be aware of: C++/ CLI = C++ + .NET. You have to learn a new C++ dialect which contains some key words that are used for compiling to CIL code. You must be aware of garbage collection and how this works in .NET.
Re: Access C++ dll from C#
Here is a small snippet from the sample code I've been in C++ API
Code:
#include "stdafx.h"
#include "ResultAPI.h"
#include "TerrainResultAPI.h"
int _tmain(int argc, _TCHAR* argv[])
{
std::wstring sProject = L"..\\data\\kursv2\\kursv2-1";
CResultAPI aResultAPI;
int iStat = aResultAPI.Open(sProject);
if (iStat < 0)
{
// error opening project
aResultAPI.Close();
return -1;
}
aRoadResultAPI.SetSectionselection(iAPI_SECTION_ALL);
etc
etc
How can aResultAPI be used without instantiated. That's what I do not quite understand. And how will it work if I write a wrapper as you mentioned. The above example violates everything I've learned about both the C++ and C#. This does not look like any object-oriented in all. Only a declaration that will be used? Although I do not have any knowledge of the header files but can it be that example. inclusion of ResultAPI.h makes the class for this header file (do not know the contents of the implementation file) will be instantiated when I include the header file? In such a case. How to know which header file to load the dll? DLL is located in the program area in general. Scanner only all classes and find one that implements the above header? What if there are several classes that implement the contents of the header?
Re: Access C++ dll from C#
You should be able to code in C++ fairly well before you begin to look at C++/CLI. C++ uses something called a Preprocessor. This means in practice that the document is processed by a compiler called a Preprocessor. Preprocessor before the code that is processed is sent to C++ compiler. This is not entirely accurate, but enough to get an understanding of what happens. When it says it will #include a file more or less be cut into the final code. If a header file is included multiple times, you get an error message that the elements are defined several times. This uses the #pragma once or #ifdef IN_HEADER_FILE to prevent. To include the dll files, you typically have both a .H file and a .Lib file. Note that C++ differs significantly from C# in many ways.
Re: Access C++ dll from C#
What I do not understand is this.
Code:
#include "stdafx.h"
#include "RoadResultAPI.h"
#include "VipsIntf\VipsIntf.h" // Road Model Input main class
#include "VipsIntf\BRoBens.h" // Bed Rock Bench / Rock Shelf
#include "VipsIntf\SurfNos.h" // Road Model Input Surface Description
#using <mscorlib.dll>
using namespace System;
public ref class CqBedRockBenchsWrapper
{
private:
public:
int Test()
{
CqBedRockBenchs aBedRockBench;
aBedRockBench.Read(1);
int retur= aBedRockBench.First();
return retur;
}
int First()
{
CqBedRockBenchs aBedRockBench;
int retur= aBedRockBench.First();
return retur;
}
int Read()
{
CqBedRockBenchs aBedRockBench;
aBedRockBench.Read(1);
return retur;
}
};
1. How can aBedRockBench be used without being instantiated? Looking in the debugger that it is ready. almost as if it is instantiated ...
2. How is the data ready in aBedRockBench when not filled from any place. Beyond that I have been running load method in another API class.
3. Every time I run CqBedRockBenchs aBedRockBench; reset aBedRockBench. This method allows the test works fine. but if I run this via two methods (via C #) and First Read. something I want then aBedROckBench zero Prepared and First returns 0 If I do both calls the same method returns 1 These calls, I will of course keep the 1 to 1
4. Inability to declare CqBedRockBenchs aBedRockBench globally. I will then get the following errors:
Error 2 error C4368: can not define 'aBedRockBench' as a member of managed 'CqBedRockBenchsWrapper': mixed types are not supported g: \ API \ ViaNova \ Nova Point Road API \ 18.00 \ examples \ TestApp_RoadModel_Input \ CqBedRockBenchsWrapper.cpp 15 TestApp_RoadModel_Input
if I declare it so
private:
CqBedRockBenchs aBedRockBench;
Re: Access C++ dll from C#
1. C++ native code functions instantiated if they write like that. Default constructor is called.
2. View 1
3. Because the variable is instantiated each time the function calls performed (but it seems that you have fixed)
4. Also fixed it seems
It is very important to be aware of pointers in C++. If you do not have * or ^ in front of a variable name the object is allocated on the stack and instantiated with the default constructor (if any) If the class has no default constructor, so the content will be arbitrary, and should be filled out manually.