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.
LVL 1
riaz9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

joachimcCommented:
Why don't you do like this?

create a button and a textbox.

in the button_click try this

text1.text = app.path

/Joachim
TimCotteeHead of Software ServicesCommented:
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
joachimcCommented:
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

wsh2Commented:
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>.
riaz9Author Commented:
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.
riaz9Author Commented:
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.
riaz9Author Commented:
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.
wsh2Commented:
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>.
DanRollinsCommented:
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
riaz9Author Commented:
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
DanRollinsCommented:
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
riaz9Author Commented:
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
Computer101Commented:
Placed in PAQ

Computer101
E-E Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.