views:

281

answers:

2

Hi all,

I want to use Run("someProgram.exe") command using BASIC language. This will open the 3rd application program.

If that program running using .dll(not sure if it possible) not .exe, how can i code that?

thanks in advance.

+2  A: 

If you want to execute a DLL function from the command line, use: Rundll.exe

Note that your exported function is required to match exactly the (C) signature:

void CALLBACK EntryPointW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow);

If it does not, it is undefined as to what will happen, but you will probably crash. The normal routine for calling arbitrary functions from dlls in class VB is to use the Declare statement. You must know the signature of the function you want to call at compile time.

For example:

Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
Function GetUser()
   Dim RetVal As Integer
   Dim UserName As String
   Dim Buffer As String
   Buffer = New String(CChar(" "), 25)
   RetVal = GetUserName(Buffer, 25)
   UserName = Strings.Left(Buffer, InStr(Buffer, Chr(0)) - 1)
   MsgBox(UserName)
End Function

This code calls the "GetUserNameA" function from advapi32.dll. There are many sites devoted to listing the Declare syntax for arbitrary windows functions, so it is not typically difficult to find the correct one.

Noon Silk
Rundll.exe is used for invoking exported functions from dlls (matching a certain signature) - this is not really what the question is after
1800 INFORMATION
1800: Actually, I think you're mistaken. He's asking to execute a DLL as an app, that's what Rundll allows. Obviously he can also just call it in the language by loading it, but that's not what he's asking for.
Noon Silk
it's still not a general method for calling exported functions from dlls. Only if your exported function matches the signature: void CALLBACK EntryPointW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow); will it work
1800 INFORMATION
Okay, but it still is an answer to the question, until the poster disambiguates it.
Noon Silk
I added some more detail
1800 INFORMATION
Cheers for that, and thanks for explaining the downvote; I learned something :)
Noon Silk
Sorry for the unclear statement of my problem. The dll i'm highlight here is the one belong to program execution, where normally we can recognize them by just .exe entension. But is this solution is referring to my problem? But ill try search on it also. thanks!
A: 

Its hard to answer without understanding the nature of your .dll. Of course, there is no way to simply pop your dll into some harness and expect it to just run. It would have to be an exe for that. You could create your own shell/wrapper exe that simply acts as a process host with an entry point and then fires methods on the dll.

I'm not sure if this would fit your model but if you were in the position where you can require these dlls contain a class that implements an interface. Say IRunnable that contains a single Run() method. You could create an exe that that receives the dll as a command line argument, uses reflection to find IRunnable and then loads that class and calls Run().

Matt Wrock