Pfad der EXE ermitteln



  • Hallo ich hab ein kleines Problem bei einer Funktion.

    char szAppPath[MAX_PATH] = "";
    char szAppDirectory[MAX_PATH] = "";
    
    GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) - 1);
    
    strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, ' \\ ') - szAppPath);
    szAppDirectory[strlen(szAppDirectory)] = '\0';
    
    if (_access(szAppDirectory + "test.ini", 00) == NULL) {}
    else { MessageBox(NULL,"Test.ini not found!", "ERROR", MB_OK); exit(1); }
    

    Ich bekomme immer Fehler: '+': Zwei Zeiger können nicht addiert werden. Wie kann ich das umgehen oder was ist falsch? Danke im voraus.



  • Hi,
    ich kenn mich da zwar nicht so aus, und versteh auch nicht warum es nicht geht, aber anscheinend kann man nicht wie im .NET framework strings 'addieren' oder wie in VB '&' benutzen.

    Aber probiere es mal mit strcat.

    #include <string.h>
    #include <windows.h>
    int main() {
    char szAppPath[MAX_PATH] = "";
    char szAppDirectory[MAX_PATH] = "";
    GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) - 1);
    strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, ' \\ ') - szAppPath);
    szAppDirectory[strlen(szAppDirectory)] = '\0';
    
    strcat(szAppDirectory, "test.ini");
    
    if(_access(szAppDirectory, 00) == NULL) {
    }
    else {
    MessageBox(NULL,"Test.ini not found!", "ERROR", MB_OK); exit(1);
    }
    }
    


  • Danke, funktioniert ohne Probleme.



  • TCHAR, TEXT(), tchar.h ... ?!

    noobiebubie schrieb:

    Hi,
    ich kenn mich da zwar nicht so aus, und versteh auch nicht warum es nicht geht, aber anscheinend kann man nicht wie im .NET framework strings 'addieren' oder wie in VB '&' benutzen.

    C-Strings kann man so nicht verketten, das stimmt. std::string/std::wstring (C++, STL) - Objekte aber schon.



  • und das soll so tun?

    char szAppPath[MAX_PATH] = "";
    char szAppDirectory[MAX_PATH] = "";
    
    GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) - 1);
    

    effektiv übergibst du dann also größe 0 (größe von szAppPath) - 1.
    -1 also größe für einen puffer der gerade einmal um die 260 zeichen lang ist!?
    schlechte idee 🙂

    besser:

    GetModuleFileName(NULL, szAppPath, MAX_PATH);
    

    grützi
    RadiatioN



  • RadiatioN Offline schrieb:

    besser:

    GetModuleFileName(NULL, szAppPath, MAX_PATH);
    

    Besser aber noch nicht perfekt:

    TCHAR szAppPath[MAX_PATH + 1];
    GetModuleFileName(NULL, szAppPath, sizeof(szAppPath) / sizeof(szAppPath[0]));
    

Anmelden zum Antworten