Solved

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

Posted on 2006-07-11
11
342 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
  • 4
  • 4
  • 2
11 Comments
 
LVL 19

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 19

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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now