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
]