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
Solved

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

Posted on 2006-07-11
11
346 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

792 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