Toon posts:

[C] FilePath achterhalen *

Pagina: 1
Acties:

Verwijderd

Topicstarter
Hee mensen,

ik ben sinds kort overgestapt op winapi.
vroeger kon ik met de parameterline van winmain zo mijn .exe locatie achterhalen van het programma dat ik had geschreven met argvc[] enzo.

Maar hoe doe je dit met winapi? is er een functie voor? WinMain opzich krijgt niets mee...

m.v.g Robert. :)

  • EfBe
  • Registratie: Januari 2000
  • Niet online
Geript uit DemoGL, mn lib voor o.a. opengl screensavers. (Ik herinnerde me dat ik het daar eens had opgelost ;)). Code is wellicht wat gaar hier en daar, maar volgens mij doet deze snippet wat je wilt. GetCommandLine is een winapi functie. Alle namen in CAPS zijn constants.

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
char        *pWorkingDirFromReg, *pCommandLine, *pCur;
char        sWorkingDir[MAXPATHNAMELENGTH];
int     iMaxLength, iIdx;

pCommandLine=GetCommandLine();
// first argument is the path plus the executable. first check if the OS has 
// put '"' around the path.
pCur=pCommandLine;
if(*pCur=='\"')
{
    // yup. eat away the string
    for(pCur++;(*pCur!=NULL) && (*pCur!='\"');pCur++){};
    if(!*pCur)
    {
        // ending '"' not found. no legitimate path found. 
        return;
    }
    // pCur points to ending '"'. 
    iMaxLength = pCur-(pCommandLine+1);
    if(iMaxLength>MAXPATHNAMELENGTH)
    {
        iMaxLength=MAXPATHNAMELENGTH;
    }
    strncpy(sWorkingDir,pCommandLine+1,iMaxLength);
}
else
{
    // no string markers. this means there are no spaces in the path+filename. find the first
    // space and copy string before that space to the workingdir string
    for(;(*pCur!=NULL) && (*pCur!=' ');pCur++){};
    // routine quits with either pCur at the end of the string or at the first space.
    iMaxLength = pCur-pCommandLine;
    if(iMaxLength>MAXPATHNAMELENGTH)
    {
        iMaxLength=MAXPATHNAMELENGTH;
    }
    strncpy(sWorkingDir,pCommandLine,iMaxLength);
}
// now, walk back from the back of the string, until we find the first '\'. put there a '\0'
for(iIdx=strlen(sWorkingDir)-1;iIdx>=0;iIdx--)
{
    if(sWorkingDir[iIdx]=='\\')
    {
        // found it
        sWorkingDir[iIdx]='\0';
        break;
    }
}
if(iIdx==0)
{
    // not a path found. return
    return;
}

[ Voor 6% gewijzigd door EfBe op 20-05-2004 09:58 ]

Creator of: LLBLGen Pro | Camera mods for games
Photography portfolio: https://fransbouma.com


Verwijderd

Hier kan je denk ik beter GetModuleFileName voor gebruiken.

Verwijderd

exact, dat scheelt weer een Kb of wat aan code (en RSI)

Verwijderd

Topicstarter
hartelijk bedankt jullie 2.
Vooral die 2e was erg handig, ik zal die functie onthouden:

char filename[100];
GetModuleFileName(0,filename,100);
MessageBox(0,filename,0,0);

  • EfBe
  • Registratie: Januari 2000
  • Niet online
Verwijderd schreef op 20 mei 2004 @ 10:02:
Hier kan je denk ik beter GetModuleFileName voor gebruiken.
Heb ik die hele functie voor zitten proggen, zit het gewoon in win32... :D. Naja, boeie verder :)

Creator of: LLBLGen Pro | Camera mods for games
Photography portfolio: https://fransbouma.com


  • curry684
  • Registratie: Juni 2000
  • Laatst online: 12-05 22:23

curry684

left part of the evil twins

EfBe schreef op 20 mei 2004 @ 11:15:
[...]

Heb ik die hele functie voor zitten proggen, zit het gewoon in win32... :D. Naja, boeie verder :)
Ik moet zeggen dat ik het wel een sterke functie vind verder hoor, maar over het algemeen kun je bij Win32 wel als stelregel stellen dat als je voor iets simpels meer dan 5 regels code moet typen er al een functie voor is ;)

Professionele website nodig?

Pagina: 1