Click here to Skip to main content
15,844,498 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I got an exception of "EntryPointNotFoundException" when compiling VB 2005 solution. The error message is:

"Unable to find an entry point named 'Sample_Initialize' in DLL 'Sample.dll'."

Below is my P/Inovke method to call unmanaged code.

VB
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Sample.Sample_Initialize()
    End Sub

Public Class Sample
    Public Declare Sub Sample_Initialize Lib "Sample.dll" ()
End Class


I ran dumpbin.exe and got the following results from "Visual Studio 2005 command prompt".

c:\>dumpbin.exe /exports "c:\Sample.dll"
21 14 00001010 _Sample_Initialize@0

Could you advise if there is syntax error when using P/Invoke method on Sample.dll?
Posted
Comments
Sergey Alexandrovich Kryukov 6-May-11 22:47pm    
You almost got it, my 4. You only had to read about DllImport more thoroughly.
--SA

Isn't that already explained?

The exported name is "_Sample_Initialize@0", not "Sample_Initialize". Such strange exported name is a result of C++ name mangling. Such name cannot be a valid C# identifier name. Here is how to deal with this situation: The name if the external function can be anything, but the property EntryPoint of the attribute System.Runtime.InteropServices.DllImportAttribute should be "_Sample_Initialize@0":

VB
<DllImport("Sample.dll", EntryPoint := "_Sample_Initialize@0")>
' external function declaration:
Sub SampleInitialize() ' ...


Using of a binary dump of the native library for name resolution and checking up of what's exported is absolutely right idea! Dumpbin.exe comes to help.

For more detail, read:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx#Y1916[^],
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.entrypoint.aspx[^] (see a code sample).

—SA
 
Share this answer
 
v5
Comments
bj93081 8-May-11 19:57pm    
Thanks for your kind comments. VB did not like entry point name with "@". Thus, I used Alias to work around in the solution I posted.
Sergey Alexandrovich Kryukov 8-May-11 20:42pm    
Of course it would be not a good name. But you don't even need an alias (however it would work). As name EntryPoint "_Sample_Initialize@0" is separate from the interfaced name, I called it SampleInitialize. It will be bound by "_Sample_Initialize@0" but known in VB.NET as SampleInitialize. This is very usual technique.
--SA
bj93081 9-May-11 2:59am    
I tried your suggestions below. However, I got an error "SampleInitialize is not a member of Sample.DLL." when calling SampleInitialize in Public Class Sample. Any clue?

Imports System.Runtime.InteropServices
Module Example
DllImport("Sample.dll", EntryPoint := "_Sample_Initialize@0")> _
Sub SampleInitialize()
End Sub
End Module
Sergey Alexandrovich Kryukov 9-May-11 3:30am    
I don't have VB.NET installed to check it. I test on C# and use VB.NET syntax, so my code is not finished. Please look at the samples in my references and use the syntax for declaration of the method, but use my line with DllImport. Did you try with Alias?
--SA
bj93081 9-May-11 12:29pm    
That's fine. Alias would work. Thanks.
Here is the solution my friend found. As SA pointed out, the exported name is "_Sample_Initialize@0", not "Sample_Initialize". In addition, Alias needs to be added to work around wild card characters since VB did not like it.

VB
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Sample.Sample_Initialize()
    End Sub

VB
Public Class Sample
Public Declare Sub Sample_Initialize Lib "Sample.dll" Alias "_
_Sample_Initialize@0" ()
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900