[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2006-07-11
11
Medium Priority
?
369 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 1000 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

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!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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