Getting an error that entry point can not be located from my pinvo

G

Guest

I'm trying to pinvoke a function in a C++ dll but I'm getting an error that
entry point can not be located from my pinvoke code. Do I need to add any
code to the C++ DLL to make the pinvoke? The following is what I have in my
C# code:

[DllImport("PropSheetHost.dll")]
public static extern void ShowAdProp(
[MarshalAs(UnmanagedType.LPStr)]
String m);

//This is where I invoke the function
private void propertyToolStripMenuItem_Click(object sender,
EventArgs e)
{

ShowAdProp("'CN=1608,CN=Users,CN=Accounting,CN=Contexts,CN=Unity,CN=Symark,CN=Program Data,DC=unity,DC=windev,DC=symark,DC=com");
}

//This is start of the C++ dll
using namespace std;

void ShowAdProp(wstring adPath)
{

CoInitialize(NULL);

HRESULT hr;

HINSTANCE hInstance = NULL;
HWND hwndConsole = GetConsoleWindow();
if(hwndConsole)
{
hInstance = (HINSTANCE)(LONG_PTR)GetWindowLongPtr(hwndConsole,
GWLP_HINSTANCE);
}

CPropSheetHost *pHost = new CPropSheetHost(hInstance);

// Hold a reference count for the CPropSheetHost object.
pHost->AddRef();

hr = pHost->SetObject(adPath.c_str());
 
M

Mattias Sjögren

Do I need to add any
code to the C++ DLL to make the pinvoke?

Not add but change. The parameter type can't be a wstring, it must be
a library neutral type such as a char*. You must also ensure you're
using the right calling convention and check under which name the
function is exported (it may be mangled).


Mattias
 
G

Guest

Thank you for the reply. Here is what I've modified but I'm still getting
the same error message.


//The header file
public:
HRESULT SetObject(LPCWSTR pwszADsPath);
HRESULT SetObject(IADs *pads);
__declspec(dllexport) void ShowAdProp(WCHAR* inPath);
void Run();


//The function I'm exporting
void ShowAdProp(WCHAR* inPath)
{
wstring adPath = inPath;

CoInitialize(NULL);

HRESULT hr;

HINSTANCE hInstance = NULL;
HWND hwndConsole = GetConsoleWindow();
if(hwndConsole)
{
hInstance = (HINSTANCE)(LONG_PTR)GetWindowLongPtr(hwndConsole,
GWLP_HINSTANCE);
}

CPropSheetHost *pHost = new CPropSheetHost(hInstance);

// Hold a reference count for the CPropSheetHost object.
pHost->AddRef();

hr = pHost->SetObject(adPath.c_str());
if(FAILED(hr))
{
goto ExitMain;
}

pHost->Run();

/*
Release the CPropSheetHost object. Other components may still hold a
reference to the object, so this cannot just be deleted here. Let
the object delete itself when all references are released.
*/
pHost->Release();


ExitMain:

CoUninitialize();
}

//My import statement
namespace ppGlobal
{
public partial class FormMain : Form
{
[DllImport("PropSheetHost.dll")]
public static extern void ShowAdProp(
[MarshalAs(UnmanagedType.LPWStr)]
String m);

//Invoke statement
private void propertyToolStripMenuItem_Click(object sender,
EventArgs e)
{

ShowAdProp("'CN=1608,CN=Users,CN=Accounting,CN=Contexts,CN=Unity,CN=Symark,CN=Program Data,DC=unity,DC=windev,DC=symark,DC=com");
}

//Error Message
An unhandled exception of type 'System.EntryPointNotFoundException' occurred
in ppGlobal.exe

Additional information: Unable to find an entry point named 'ShowAdProp' in
DLL 'PropSheetHost.dll'.
 
W

Willy Denoyette [MVP]

The PInvoke marshaler cannot marshal a String to a std::wstring object.
Change your C++ code and make the argument a simple ascii string or a
wchar_t string.


Willy.

| I'm trying to pinvoke a function in a C++ dll but I'm getting an error
that
| entry point can not be located from my pinvoke code. Do I need to add any
| code to the C++ DLL to make the pinvoke? The following is what I have in
my
| C# code:
|
| [DllImport("PropSheetHost.dll")]
| public static extern void ShowAdProp(
| [MarshalAs(UnmanagedType.LPStr)]
| String m);
|
| //This is where I invoke the function
| private void propertyToolStripMenuItem_Click(object sender,
| EventArgs e)
| {
|
|
ShowAdProp("'CN=1608,CN=Users,CN=Accounting,CN=Contexts,CN=Unity,CN=Symark,CN=Program
Data,DC=unity,DC=windev,DC=symark,DC=com");
| }
|
| //This is start of the C++ dll
| using namespace std;
|
| void ShowAdProp(wstring adPath)
| {
|
| CoInitialize(NULL);
|
| HRESULT hr;
|
| HINSTANCE hInstance = NULL;
| HWND hwndConsole = GetConsoleWindow();
| if(hwndConsole)
| {
| hInstance = (HINSTANCE)(LONG_PTR)GetWindowLongPtr(hwndConsole,
| GWLP_HINSTANCE);
| }
|
| CPropSheetHost *pHost = new CPropSheetHost(hInstance);
|
| // Hold a reference count for the CPropSheetHost object.
| pHost->AddRef();
|
| hr = pHost->SetObject(adPath.c_str());
|
| --
| Thanks.
 
G

Guest

Thank you and I did that as follow but I'm still getting the same error
message.
__declspec(dllexport) void ShowAdProp(WCHAR* inPath)
 
C

Claes Bergefall

I'm guessing that the name gets mangled. Check to make sure that the
exported name is really ShowAdProc (by using depends.exe for instance). You
also need to change your MarshalAs statement in the DllImport to
UnmanagedTypes.LPWStr.

/claes


Pucca said:
Thank you and I did that as follow but I'm still getting the same error
message.
__declspec(dllexport) void ShowAdProp(WCHAR* inPath)
--
Thanks.


Willy Denoyette said:
The PInvoke marshaler cannot marshal a String to a std::wstring object.
Change your C++ code and make the argument a simple ascii string or a
wchar_t string.


Willy.

| I'm trying to pinvoke a function in a C++ dll but I'm getting an error
that
| entry point can not be located from my pinvoke code. Do I need to add
any
| code to the C++ DLL to make the pinvoke? The following is what I have
in
my
| C# code:
|
| [DllImport("PropSheetHost.dll")]
| public static extern void ShowAdProp(
| [MarshalAs(UnmanagedType.LPStr)]
| String m);
|
| //This is where I invoke the function
| private void propertyToolStripMenuItem_Click(object sender,
| EventArgs e)
| {
|
|
ShowAdProp("'CN=1608,CN=Users,CN=Accounting,CN=Contexts,CN=Unity,CN=Symark,CN=Program
Data,DC=unity,DC=windev,DC=symark,DC=com");
| }
|
| //This is start of the C++ dll
| using namespace std;
|
| void ShowAdProp(wstring adPath)
| {
|
| CoInitialize(NULL);
|
| HRESULT hr;
|
| HINSTANCE hInstance = NULL;
| HWND hwndConsole = GetConsoleWindow();
| if(hwndConsole)
| {
| hInstance = (HINSTANCE)(LONG_PTR)GetWindowLongPtr(hwndConsole,
| GWLP_HINSTANCE);
| }
|
| CPropSheetHost *pHost = new CPropSheetHost(hInstance);
|
| // Hold a reference count for the CPropSheetHost object.
| pHost->AddRef();
|
| hr = pHost->SetObject(adPath.c_str());
|
| --
| Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top