Solved

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

Posted on 2006-07-11
11
343 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is setting this variable making a duplicate in memory 4 30
Angular JS Route 3 52
Reset asp.net login password 4 32
How can my static class become undefined?? 8 18
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

16 Experts available now in Live!

Get 1:1 Help Now