Solved

Got error that entry point can not be located from my pinvoke code

Posted on 2006-07-11
11
349 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
11 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 17087110
0
 

Author Comment

by:lapucca
ID: 17087129
I didn't add/change any of the code of ShowAdProop().  How do I make it an exported library?  Thanks.
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 17087161
Export from a DLL Using __declspec(dllexport)
Home |  Overview |  How Do I |  FAQ |  Details |  Sample

You can export data, functions, classes, or class member functions from a DLL by using the __declspec(dllexport) keyword. If you use __declspec(dllexport), you do not need a .DEF file for exports.

To export functions, the __declspec(dllexport) keyword must appear to the left of the calling-convention keyword, if a keyword is specified. For example:

__declspec(dllexport) void __cdecl Function1(void);

To export all the public data members and member functions in a class, the keyword must appear to the left of the class name as follows:

class __declspec(dllexport) CExampleExport : public CObject
{ ... class definition ... };

When building your DLL, you typically create a header file that contains the function prototypes and/or classes you are exporting, and add the __declspec(dllexport) to the declarations in the header file. To make your code more readable, define a macro for __declspec(dllexport) and then use the macro with each symbol you are exporting:

#define DllExport   __declspec( dllexport )

__declspec(dllexport) stores function names in the DLL’s export table. If you want to optimize the table’s size, see Export Functions From a DLL By Ordinal Rather Than By Name.

Note   When porting DLL source code from Win16 to Win32, replace each instance of __export with __declspec(dllexport).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 250 total points
ID: 17088115
1) Using dumpbin utility with /exports switch test whether ShowAdProp function is actually exported from Dll. Follow recommendations from Dan7el's post to fix this. Remember that you need to use extern "C" to prevent C++ names mangling.

2) [MarshalAs(UnmanagedType.LPStr)]
It looks like you need LPWStr

3) void ShowAdProp(wstring adPath)
In any case, such function cannot be called by C# client, because it doesn't have idea what is wstring class. Replace it with plain string pointer WCHAR*.
0
 

Author Comment

by:lapucca
ID: 17092622
Thank you for all the reply.  I've add and modify the following code but I'm still getting the same error.
//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'.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17092738
This means, function is not exported. Test this with dumpbin program. Read again post about __declspec(dllexport).
0
 

Author Comment

by:lapucca
ID: 17092753
Where would I add the exter "C"?  Thank you.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17092880
extern "C"
__declspec(dllexport) void ShowAdProp(WCHAR* inPath);
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17092897
See MSDN article "Using extern to Specify Linkage".
0
 

Author Comment

by:lapucca
ID: 17102613
Thanks everyone, I'm still working on it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question