Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I already know how to import a dll into my project but by manually reference im receiving errors like "entry point was not found"

I created the dll file called myDll the class is called myFunctions and a function called showResult() As String just for example:

myDll.dll which is located on C:\ for example

VB
Public Class myFunctions
  Public Function showResult() as string
  Return "HELLO WORLD"
  End Function
End Class



myApplication

VB
Public Class callExternDll
  <dllimport("c:\mydll.dll")>
  Public Shared Function showResult() As String
  End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MsgBox(showResult)

End Sub



I want to call the function so to receive "hello world"
Posted
Updated 15-Oct-12 5:47am
v2
Comments
Kuthuparakkal 15-Oct-12 11:41am    
do you have Sub Main in your program ? like this :
' Allow easy reference to the System namespace classes.
Imports System

' This module houses the application's entry point.
Public Module modmain
' Main is the application's entry point.
Sub Main()
' Write text to the console.
Console.WriteLine ("Hello World using Visual Basic!")
End Sub
End Module
[no name] 15-Oct-12 11:46am    
No i havent because its a class library and not a module so i thought i dont have to write it, but as you can see above there is at least one Function which id called showResult() so this must be an entry point too or am i wrong? do i have to rename it into sub showResult???
Sergey Alexandrovich Kryukov 15-Oct-12 13:25pm    
The question is: how about the first fragment, which you probably written to export? Is it written as native code, or CLI (using VB.NET)?
--SA
[no name] 15-Oct-12 13:36pm    
i dont know i just created it using Class Library and builded it!
Sergey Alexandrovich Kryukov 15-Oct-12 14:06pm    
If you don't know what are you using and what is your platform, learn it first, before doing any programming. Or don't program at all, as it's useless. Know what you do.
--SA

1 solution

Please see my comment to the question. If myFunctions.showResult is also in the CLI code, written in VB.NET, you don't need P/Invoke to use it. P/Invoke is only used to use unmanaged code in .NET (or other CLI) assembly. If both pieces are .NET assemblies, you simply reference one by another and use it directly:
http://en.wikipedia.org/wiki/.NET_assembly[^],
http://msdn.microsoft.com/en-us/library/ms973231.aspx[^].

If you use unmanaged code in a .NET assembly, you really need to use P/Invoke. One thing you should check up is using right entry point name, calling conventions (which should be the same between different versions of Basic), etc. When you have a DLL, you can see its exports using some binary dump tool like "dumpbin.exe". Please see:
http://msdn.microsoft.com/en-us/library/aa446532.aspx[^].

If you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be useful: Essential P/Invoke[^].

—SA
 
Share this answer
 
Comments
[no name] 15-Oct-12 14:01pm    
Thank you for the reply but i even didnt know that i was using PInvoke i just found the codelines in google.
Yes my .dll i wrote is created in vbnet (VS2010) and it should be in managed code i reviewed and didnt found the options "CLI" or "Native"
i just created a project, selected Class Library and builded it and a .dll came out.
Now the problem is something else, because when i import the same dll onto my project(windows application) and then
Imports mydll.dll
dim abc as new mydll.dll
then the function on it immediately was displayed but when i try to load it while running without i have imported that mydll.dll before and i try to refer to it.
So if i import the dll before creating my app it works but by reference while running it sends errors forwards like PInvoke Restriction: cannot return variants
Please help if you have any idea,
Regards, Begi
Sergey Alexandrovich Kryukov 15-Oct-12 14:07pm    
Don't tell "I don't know", come to knowing it. I provided you the references. Read.
--SA

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