Results 1 to 6 of 6

Thread: Access C++ dll from C#

  1. #1
    Join Date
    Jun 2009
    Posts
    59

    Access C++ dll from C#

    To use a C++ dll from C#, previously I have done this through
    1. COM which is just like using a C# dll is my impression
    or
    2. something like this
    [DllImport ("filename.dll")]
    public static extern void start ();

    My problem is again there is a COM component as well as the methods are in classes. And do not think it can be accessed through the methods declared in the DEF file.

    I have access to DLL which is attached to the header files for the classes I'm looking to spend. And all the classes' methods include only basic data (int, double, string, etc) and not classes, etc. Have a test project in C++ using DLL in a way. But it uses only in this way. The class that is referenced below is located in the header file. but the strange thing for me to come with C# background, it is not instantiated before use.
    Code:
    Class k;
    int return =k.Load("filename");
    if(return <0)
    (
    //Give Error Message
    )
    etc.
    etc.
    Have heard a few places that I can write a wrapper in C++ (do not code for the DLL. Only the header files). Make this COM compatible and can use this from C#. This is the emergency solution if I do not have the ability to use C# directly. It comes with a lot of strange information and a lot of weird issues here for sure but hope someone understands what I'm looking for and can give me help with this. Can also be said that the DLL in question is a dll purchased as part of an API. But do not know if it is possible and perhaps how this can be used from C#.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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.

  3. #3
    Join Date
    Jun 2009
    Posts
    59

    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?

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    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.

  5. #5
    Join Date
    Jun 2009
    Posts
    59

    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;

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    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.

Similar Threads

  1. Replies: 3
    Last Post: 09-01-2014, 10:06 AM
  2. Replies: 22
    Last Post: 03-05-2012, 02:06 PM
  3. Replies: 3
    Last Post: 19-12-2010, 07:57 AM
  4. Replies: 3
    Last Post: 11-12-2010, 11:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,665,154,639.55801 seconds with 17 queries