Question about C++ DLL function use in C#
I have a DLL made in C++ that is then used in a C# program. the C# program references the DLL and i call the classes and can use the functions defined in the DLL.
Now i want to add other functions to the DLL so i can run C++ code in C#. I Follow the example of how the other functions in the DLL are defined; .h and .cpp and i create a new function that executes code. I put it in the same places as the previously define functions, but when i put the DLL in my C# code my function is not there. when i create an instance of the DLL class my function is not a part of it.
I put it in the .h as a public function of the class and gave it code to run in the .cpp but its not found.
Why can't my C# program see the function i made even though its following the same suite as the other functions?
UPDATE
code.h:
#pragma once
#include <windows.h>
#include <PvDisplayWnd.h>
#include <vfw.h>
#include "NPvResult.h"
#include "NPvDeviceInfo.h"
#include "NPvBuffer.h"
#include <string>
using namespace System;
using namespace System::Runtime::InteropServices;
public ref class NPvDisplayWnd
{
public :
NPvDisplayWnd();
bool Handle();
NPvResult^ ShowModeless(long locx, long locy, long x, long y);
NPvResult^ ShowModal();
NPvResult^ NPvDisplayWnd::Create();
int^ Display( NPvBuffer^ aBuffer, int^ x);
NPvResult^ Work( const std::string afilename, unsigned short asizex, unsigned short asizey, unsigned short aBPP, double aFPS);
NPvResult^ Close();
NPvResult^ DoEvents();
}
code.cpp:
#include "NPvDisplayWnd.h"
NPvDisplayWnd::NPvDisplayWnd(){code}
NPvResult^ NPvDisplayWnd::Create(){code}
bool NPvDisplayWnd::Handle()
NPvResult^ NPvDisplayWnd::ShowModeless(long locx, long locy, long x, long y){code}
NPvResult^ NPvDisplayWnd::ShowModal(){code}
int^ NPvDisplayWnd::Display( NPvBuffer^ aBuffer, int^ x){code}
NPvResult^ NPvDisplayWnd::DoEvents(){code}
NPvResult^ NPvDisplayWnd::Work( c开发者_如何转开发onst std::string aFileName, unsigned short aSizeX, unsigned short aSizeY, unsigned short aBPP, double aFPS){code}
NPvResult^ NPvDisplayWnd::Close(){code}
The function that i added was the Work() function. if i build this and place it in my C# code, it sees all the functions except work. To make sure i was using the right .dll, i changed the name of ShowModeless() to ShowModeless__F() and rebuilt it and added it to my C# and the change carried over, but i still didn't see my Work() function.
On Windows, you must explicitly export symbols in order for users to be able to access them via DLLs. This is done using the __declspec(export)
syntax. This is needed during compilation and linking of the DLL.
Now that we know you're using C++/CLI instead of regular C++, we can see a problem. Work
takes a std::string
. But Work
is intended to be a managed function, and managed code cannot take a std::string
as a parameter. You need to take a managed string type instead.
A native C++ class cannot be imported in C#. You need to utilize C++/CLI feature in existing C++ DLL so that managed clients (like C#) can import the classes.
namespace XX
{
public ref class YourClass{...};
}
It will export managed class YourClass
under XX
namespace. You need to use /CLR
compiler flag.
You may compile set of CPP files as managed, and set of other files as unmanaged. Managed classes cannot contain unmanaged stuff (unless it is declared pointer).
精彩评论