Link to home
Start Free TrialLog in
Avatar of riaz9
riaz9

asked on

Find out the command line

can somebody help me how to use the GetCommandLine function of Win32 API in Visual Basic?  My VB Application crashes whenever i call this function. I want to find out the complete path from where my program has started. I have the same problem when i use the GetStartupInfo function and try to access returned data.
Avatar of joachimc
joachimc

Why don't you do like this?

create a button and a textbox.

in the button_click try this

text1.text = app.path

/Joachim
Joachimc, you are hardly a new EE member so should be aware of the guidelines on comments vs answers. Please re-read this at the bottom of this page. Whilst I would agree that app.path may well return the desired result it does not necessarily answer the question as posted.

Nor in fact does this but it is a nicer way of getting the same information. I too have had problems with the GetCommandLine call though I have managed to get it to work occasionally. One thing to remember with GetCommandLine is that it doesn't necessarily return the full path on windows NT.

Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer
Const GWW_HINSTANCE = (-6)
Private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim ModuleName As String, FileName As String, hInst As Long
    'create a buffer
    ModuleName = String$(128, Chr$(0))
    'get the hInstance application:
    hInst = GetWindowWord(Me.hwnd, GWW_HINSTANCE)
    'get the ModuleFileName:
    'enter the following two lines as one, single line:
    ModuleName = Left$(ModuleName, GetModuleFileName(hInst, ModuleName, Len(ModuleName)))
    'set graphics mode to persistent
    Me.AutoRedraw = True
    'show the module filename
    Me.Print "Module Filename: " + ModuleName
End Sub
I am very well aware of the guide lines. But I am also aware of the anarchy that exists when choosing answers to accept a comment. There is really no solution to that today.
Why are you using APIs?

The Program startup directory is stored in:

App.Path

and the VB

COMMAND

function will return the command line parameters as a string, which you can then parse according to your needs.

==================================================
joachimc.. I wholeheartedly agree with TimCottee.. the only anarchy in this forum.. is YOU for locking down this question, precluding riaz9 from getting a full discussion of his/her question. Please change your Answer to a Comment.

riaz9.. if you reject joachimc's answer.. your question will be retuned to the much more well traveled areas of this forum.. affording you the opportunity for your question to recieve the proper amount of consideration it deserves. IF, at a later time.. you feel that joachimc's comment is the most apprpriate to your cause.. you can then accept it as the answer.. regardless of the fact that it was once rejected.. <smile>.
Avatar of riaz9

ASKER

thanks a lot for the proposed answer joachimc, your solution gives me the appl. path, but it does not tell me how to exactly use the getcommandline function to get the same result (i am aware that in NT, the full appl. path is not returned).  The actual problem that i am facing here, is that when i execute GetCommandLine or GetStartupInfo function and access the results returned.  Please help me as to how to use this functions without my program terminating.
Avatar of riaz9

ASKER

thanks a lot for the proposed answer joachimc, your solution gives me the appl. path, but it does not tell me how to exactly use the getcommandline function to get the same result (i am aware that in NT, the full appl. path is not returned).  The actual problem that i am facing here, is that when i execute GetCommandLine or GetStartupInfo function and access the results returned, my application crashes.  Please help me as to how to use this functions without my program terminating.
Avatar of riaz9

ASKER

thanks a lot for the proposed answer joachimc, your solution gives me the appl. path, but it does not tell me how to exactly use the getcommandline function to get the same result (i am aware that in NT, the full appl. path is not returned).  The actual problem that i am facing here, is that when i execute GetCommandLine or GetStartupInfo function and access the results returned, my application crashes.  Please help me as to how to use this functions without my program terminating.
As you are active again here.. what is your status with this Question? As I pointed out above.. there is NO need for APIs as the VB Command function will give you the Command Line the program was started.

PLEASE tend to your Question here with a comment, BEFORE proceeding with your Aug 25 Questions.. <smile>.
Avatar of DanRollins
Hi riaz9@devx,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Refund points and save as a 0-pt PAQ.
*** useful comments, but question is clear... and was not answered.

EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
Avatar of riaz9

ASKER

Hi DanRollins,

I am somewhat confused here now.  My main problem here was the way the functions GetCommandLine and GetStartupInfo were working, so thats the reason why i had put these here.  I wanted to know what i was doing wrong that these functions were not working.

Are you suggesting that i reduct points to 0 and accept joachimc's answer? (because this did not solve my problem of my application crashing?)

Or do I post a request to delete the question?

Please help me how to proceed!

Thanks
Riaz
Hi Riaz,
We are cleaning up these old question.  In general, it is up the the Asker (you) to try to finalize them, but if you stop responding to Expert comments, we assume that you are unavailable and the Comminity Support Moderators come along and force a decision.

At this point, it is unlikely that you will get your question answered.  So your options are:

o Accept one of the above comments as an answer (in case you want to reward an expert even though you did not get a solution).

o Post a question to Community Support and ask them to delete this question.

o Just do nothing.  In one week a Moderator will pass through here and refund your points; the Mod will reduce the points to 0 and save this question in the PAQ because there is some good information here.

--- If you still need an answer, it would be best to post a new question in this Topic Area.  That will get attention.  This one will probably not.

-- Dan
Avatar of riaz9

ASKER

Thanks for the reply. I will wait for a week for the moderator to reduce the points and save question as PAQ - because as you said this question might really help others (who want to know what the command line for their vb program is).

Riaz
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial