title: using C++ DLL in C# windows application:Getting error "Entry point not found"
body: <p>I am new to using C++ libraries in C# and also to the C++ programming in general. I have a DLL built from a C++ code which I believe is a 'managed' code as the name of the DLL is "TestManaged.dll". I am not 100% sure if the dll/C++ code is managed/unmanaged.</p>
  
  <p>I want to use classes and methods of this DLL in my <code>C# windows forms application</code> code. There are multiple classes in this DLL. When I chekced these classes and methods inside those classes in <code>Object Browser</code>, all of them have <code>Public</code> identifier.</p>
  
  <p>So far, I have added this DLL to my references of C# application code. There are three classes I would talk about in my question: <code>Product</code>, <code>ReqStatus</code>, <code>ProductData</code>. I could create an object(s) for various classes of this DLL as follows. </p>
  
  <pre><code>Product testCall = new ProductClass();
  </code></pre>
  
  <p>There is another class called <code>ProductData</code> in this DLL and I could get the C++ code for this class which is as follows. In this case, <code>ProductData</code> is shown as class in <code>Object Browser</code> in C# where as it is actually a struct in C++ code. I am not sure if this is important to answer my question (at the end). </p>
  
  <p>Following is a C++ code that defines <code>ProductData</code> struct - <code>ProductData.h</code> file. </p>
  
  <pre><code>#ifdef WIN32_MANAGED 
  public ref  struct ProductData
  #else
  struct ProductData
  #endif
  {
      UINT32 ProductId;           //!&lt; Product ID    
      UINT32 PRoductRev;         //!&lt; Build Revision
  };
  </code></pre>
  
  <p>Following is a C++ code that defines <code>ReqStatus</code> enum - <code>ReqStatus.h</code> file. I have created the same enum in my C# code with no identifier specified.</p>
  
  <pre><code>enum ReqStatus
  {
      SUCCESS,            //!&lt; Method was successful
  
      //Connection errors
      NOT_CONNECTED,      //!&lt; Connection not open 
      CONN_TIMEOUT,       //!&lt; Connection timed out commuincating with device
  };
  </code></pre>
  
  <p>Now, there are two methods I want to call and have problems with both:</p>
  
  <p>Method 1: is a <code>getProductData</code> method inside <code>Product</code> class which accepts object of <code>ProductData</code> type as a parameter and returns the <code>ReqStatus</code> which is an enum type in C++. So following is the declaration of the <code>gerProductData</code> method (as seen in the <code>Object Browser</code>):</p>
  
  <pre><code>public ReqStatus getProductData(ProductData data)
  </code></pre>
  
  <p>The same method's C++ delcaration is: (The actual method is too long and hence just giving the declaration): This method is inside <code>Prodcut.cpp</code> file</p>
  
  <pre><code>ReqStatus Product::getProductData(ProductData PLATFORM_PTR data)
  </code></pre>
  
  <p>PLATFORM_PTR is defined as below in <code>Platform.h</code></p>
  
  <pre><code>#ifdef WIN32_MANAGED
  #define PLATFORM_PTR ^
  #else
  #define PLATFORM_PTR *
  #endif
  </code></pre>
  
  <p>Method 2: is a <code>getConnected</code> method inside <code>Product</code> class which accepts a character array (I am not sure of this) and an object of <code>ProductData</code> type as a parameter and returns the <code>ReqStatus</code> which is an enum type in C++. So following is the declaration of the <code>getConnected</code> method (as seen in the <code>Object Browser</code>):</p>
  
  <pre><code>public ReqStatus getConnected(sbyte* someChar, ProductData data)
  </code></pre>
  
  <p>The same method's C++ delcaration is: (The actual method is too long and hence just giving the declaration): This method is inside <code>Prodcut.cpp</code> file</p>
  
  <pre><code>ReqStatus Product::getConnected(const char *someChar, ProductData PLATFORM_PTR data)
  </code></pre>
  
  <p>C++ code calls the methods as follows:</p>
  
  <pre><code>private : Product^  _testProduct;
  testProduct = gcnew Product();
  ProductData ^ data = gcnew ProductData();
  
  int portNum = Decimal::ToInt16(7); 
  char portName[32];
  _snprintf(&amp;portName[0], sizeof(portName),"COM%d", portNum);
  ReqStatus status = _testProduct-&gt;getConnected(&amp;portName[0], data); //Calling getConnected
  </code></pre>
  
  <p>There is an internal call to <code>getProductData</code> method inside the <code>getConnected</code> method. </p>
  
  <pre><code>ReqStatus status = getProductData(data); //data is the same which was passed to the getConnected method
  </code></pre>
  
  <p>MY C# code is as follows and I got errors at both method calls: I have put errors on the same line in the below code snippet. Both methods are independent. Its just that the <code>getProductData</code> is called from <code>getConnected</code> method in C++ code. I wanted to check if I can call both individually.  </p>
  
  <pre><code>ProductData pData = new ProductData(); // OK
  Product _testProduct = new Product();  // OK
  
  ReqStatus status1 = _testProduct.getConnected("COM5", pData ); //Error 1: The best overloaded method getConnected has some invalid arguments
  ReqStatus status2 = (ReqStatus)_testProduct.getProductData(pData ); // Error 2: Method is inaccessible due to its protection level
  </code></pre>
  
  <p>For Error 1, I tried solutions from various articles on StackOverflow and other forums but, could not solve it. Just for a reference, I tried to change the "SomePortCOM" as follows but it din't work.</p>
  
  <p><strong>UPDATE</strong>: <strong>This code works fine now and I don't see Error 1(Invalid arguments). Now, I only need to get rid of the Error 2 (Protection level error). Kindly provide any suggestions. Thank you.</strong></p>
  
  <pre><code>String str = "COM5";
  byte[] bytes = Encoding.ASCII.GetBytes(str);
  unsafe
      {
          fixed (byte* p = bytes)
          {
                  sbyte* sp = (sbyte*)p;
                  //SP is now what you want
                     ReqStatus status1 = _testProduct.getConnected(sp, pData );
          }
      }
  </code></pre>
  
  <p>For Error2, I searched so many blogs and found that one of the possible solution could be the use of DLLImport, I tried that as well and I have following issue:</p>
  
  <p>C# declaration of DLLImport:</p>
  
  <pre><code>[DllImport("TestManaged.dll",EntryPoint="getConnected")]
          public static extern ReqStatus getConnected(String SerialPort, ref ProductData pData);
  </code></pre>
  
  <p>I am calling this function as below from my C# code:</p>
  
  <pre><code>ProductData pData = new ProductData();
  String str = "COM7";
  ReqStatus status1 = getConnected(str, ref pData);
  </code></pre>
  
  <p>However, I am getting <code>Entry point not found</code> error. I tried to run the dumpbin function to get the list of functions exported by this DLL. But, I do not see any functions. Rather just a random output as below.</p>
  
  <pre><code>Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
  Copyright (C) Microsoft Corporation.  All rights reserved.
  
  
  Dump of file C:\Rumit\TestManaged.dll
  
  File Type: DLL
  
    Summary
  
          2000 .data
         22000 .rdata
          1000 .reloc
          1000 .rsrc
         13000 .text
  </code></pre>
  
  <p><strong>UPDATE: 
  Also, I do not see any methods in this DLL via Dependency Walker.
  Now, I have got the source code for C++. But I am fairly new to C++ coding. In case any change is required to C++ code, kindly give the directions.</strong> </p>
  
  <p>Regards,
  Rumit</p>