NO

Author Topic: Problem with GetModuleFileName()  (Read 18499 times)

pitter2206

  • Guest
Problem with GetModuleFileName()
« on: January 29, 2011, 07:30:38 pm »
Hi,

I try to get the name of my Exe liike that:

Code: [Select]
TCHAR szPath[MAX_PATH];

GetModuleFileName(NULL, szPath, MAX_PATH );

So far it works, my Msgbox shows me the wright path and name.

Now I want to replace the fileextension from .exe to .txt...
All I tried yet fails.

I thought, it would be possible to convert the string into char with wcstombs(), but I don´t know how to....

Or is there another way to change the extension to work with it as wchar_t???

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1940
Re: Problem with GetModuleFileName()
« Reply #1 on: January 29, 2011, 09:21:28 pm »
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

int __cdecl WinMainCRTStartup(void)
{
TCHAR szTmp[MAX_PATH];
int nLen = GetModuleFileName(NULL, szTmp, MAX_PATH);
MessageBox(0, szTmp, 0, MB_OK);
lstrcpy(&szTmp[nLen-3], TEXT("txt"));
MessageBox(0, szTmp, 0, MB_OK);
ExitProcess(0);
}
EDIT: UNICODE fix
« Last Edit: January 29, 2011, 10:15:22 pm by timovjl »
May the source be with you

pitter2206

  • Guest
Re: Problem with GetModuleFileName()
« Reply #2 on: January 29, 2011, 09:51:27 pm »
hi timovjl,

I gave it a try, but I got this error:

Code: [Select]
\main.c(108): error #2140: Type error in argument 2 to 'wcscpy'; expected 'const wchar_t *' but found 'char *'.
???

pitter2206

  • Guest
Re: Problem with GetModuleFileName()
« Reply #3 on: January 29, 2011, 09:58:32 pm »
hi timovjl,

I gave it a try, but I got this error:

Code: [Select]
\main.c(108): error #2140: Type error in argument 2 to 'wcscpy'; expected 'const wchar_t *' but found 'char *'.
???

ok, I found the bug myself:
Code: [Select]
lstrcpy(&szTmp[nLen-3], L"txt");
Thank you very much timovjl!

CommonTater

  • Guest
Re: Problem with GetModuleFileName()
« Reply #4 on: January 30, 2011, 02:34:51 am »
Add...
Code: [Select]
#include <shlwapi.h>
#pragma lib "shlwapi.lib"

And you can use the PathRenameExtension() shell function...

Code: [Select]
TCHAR Path[MAX_PATH];

GetModuleFilename(NULL,Path,MAX_PATH);
PathRenameExtension(Path,".txt");

(Don't forget the L prefix ( L".txt" ) if you are working with UNICODE enabled.

It's used quite a bit for help files with extensions .hlp or .chm
It's also used for initiliazation files with the .ini extension.

No reason you can't use it here...  

However; if you are on Win7, please be aware that it gets kinda snarky when you write data files to the Program Files sub-tree...

« Last Edit: January 30, 2011, 10:13:37 pm by CommonTater »

pitter2206

  • Guest
Re: Problem with GetModuleFileName()
« Reply #5 on: January 30, 2011, 01:32:15 pm »
Hi CommonTater,

I thought too, that it would work, but Win CE5 doesn´t work with it and PellesC runs into error: fatal error #1035: Can't find include file <shlwapi.h>.  :(

The source @timovjl  posted works so far, but I have another problem to work with this szTmp....

I have to open this in the var szTmp buffered link to my textfile....
szTmp is wchar_t and I have to use a filepointer and char.... and I don´t know how to fix that problem just now...

Without this workarround, I had a code like this:
Code: [Select]
char* fname = "\\My Flash Disk\\navigation\\skin480x272\\Addins\\Addins_Menu.txt"; // Textfile
 FILE* fp = fopen ( fname, "r" );
   
if ( fp == NULL ) // Error, if file does not exist
{
MessageBox(NULL, L"Zugehörige Textdatei fehlt!", L"Fehler", MB_SETFOREGROUND);
DestroyWindow(hwnd);
PostQuitMessage(0);
}
    while ( !feof(fp) )
{
        if ( i == N )
{
MessageBox(NULL, L"Zugehörige Textdatei ist beschädigt! ", L"Fehler", MB_SETFOREGROUND);
DestroyWindow(hwnd);
PostQuitMessage(0);
return (0);
            break;
        }
if (n != fwscanf(fp, fmtw, &buttons[i].text, buttons[i].pic, buttons[i].link))
{
  MessageBox(NULL, L"Zugehörige Textdatei ist nicht vollständig! ", L"Fehler", MB_SETFOREGROUND);
DestroyWindow(hwnd);
PostQuitMessage(0);
            break;
  }
i++;
    }
    fclose ( fp );



Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1940
Re: Problem with GetModuleFileName()
« Reply #6 on: January 30, 2011, 02:00:07 pm »
WideChar to Ansi conversion:

Win32 API: WideCharToMultiByte()
CRT: wcstombs()

Code: [Select]
char szAnsi[MAX_PATH];
wcstombs(szAnsi, szTmp, sizeof(szAnsi));
MessageBoxA(0, szAnsi, 0, MB_OK);
May the source be with you

pitter2206

  • Guest
Re: Problem with GetModuleFileName()
« Reply #7 on: January 30, 2011, 02:11:20 pm »
Thank you very much... it works fine now!!!
« Last Edit: January 30, 2011, 08:04:56 pm by pitter2206 »

CommonTater

  • Guest
Re: Problem with GetModuleFileName()
« Reply #8 on: January 30, 2011, 10:06:49 pm »
Hi CommonTater,

I thought too, that it would work, but Win CE5 doesn´t work with it and PellesC runs into error: fatal error #1035: Can't find include file <shlwapi.h>.  :(

Then you need to check your search paths in  tools-> opitions-> search paths -> include.  
If you installed at the default folder (C:\Program Files\Pelles C ) you should see...
c:\program files\pelles c\include
c:\program files \pelles c\include\win


shlwapi.h is in the include\win folder as part of the standard distribution.

While you're in there, you should maybe check your libraries folders as well...
c:\program files\pelles c\lib
c:\program files\pelles c\lib\win
c:\program files \pelles c\lib\win\gl


I don't see why it wouldn't work with CE5... it's just string manipulation.
It's prime redeeming qaulity is that it's already written for you  ;D
« Last Edit: January 30, 2011, 10:09:49 pm by CommonTater »

pitter2206

  • Guest
Re: Problem with GetModuleFileName()
« Reply #9 on: January 31, 2011, 10:49:27 am »
I checked the folders and they are already set...

But, if I include the shlwapi.h, PellesC runs into error while compiling with: fatal error #1035: Can't find include file <shlwapi.h>

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1940
Re: Problem with GetModuleFileName()
« Reply #10 on: January 31, 2011, 11:13:59 am »
WinCE 5 use WinCE subfolder not Win folder.
May the source be with you

CommonTater

  • Guest
Re: Problem with GetModuleFileName()
« Reply #11 on: January 31, 2011, 11:14:18 am »
I checked the folders and they are already set...

But, if I include the shlwapi.h, PellesC runs into error while compiling with: fatal error #1035: Can't find include file <shlwapi.h>

Did you look in the include folder and see if it's there or not?
It's odd that it wouldn't be...

CommonTater

  • Guest
Re: Problem with GetModuleFileName()
« Reply #12 on: January 31, 2011, 11:16:38 am »
WinCE 5 use WinCE subfolder not Win folder.


So then, is it there but unused or just not there? 

As in would #include "c:\program files\pelles c\include\win\shlwapi.h"  work?

(No my curiosity is peaqued, since I've never done CE5 stuff)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1940
Re: Problem with GetModuleFileName()
« Reply #13 on: January 31, 2011, 11:24:12 am »
Quote
So then, is it there but unused or just not there?
In include\WinCE there is no shlwapi.h

In lib\WinCE ceshell.lib is PathRemoveExtension() but not in headers in include\WinCE ?

« Last Edit: January 31, 2011, 11:36:00 am by timovjl »
May the source be with you

CommonTater

  • Guest
Re: Problem with GetModuleFileName()
« Reply #14 on: January 31, 2011, 11:27:11 am »
Quote
So then, is it there but unused or just not there?
In WinCE there is no shlwapi.h


So the folder isn't even there?