Pelles C forum

C language => Pocket PC and Smartphone questions => Topic started by: pitter2206 on January 29, 2011, 07:30:38 PM

Title: Problem with GetModuleFileName()
Post by: pitter2206 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???
Title: Re: Problem with GetModuleFileName()
Post by: TimoVJL 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
Title: Re: Problem with GetModuleFileName()
Post by: pitter2206 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 *'.
???
Title: Re: Problem with GetModuleFileName()
Post by: pitter2206 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!
Title: Re: Problem with GetModuleFileName()
Post by: CommonTater 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...

Title: Re: Problem with GetModuleFileName()
Post by: pitter2206 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 );


Title: Re: Problem with GetModuleFileName()
Post by: TimoVJL 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);
Title: Re: Problem with GetModuleFileName()
Post by: pitter2206 on January 30, 2011, 02:11:20 PM
Thank you very much... it works fine now!!!
Title: Re: Problem with GetModuleFileName()
Post by: CommonTater 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
Title: Re: Problem with GetModuleFileName()
Post by: pitter2206 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>
Title: Re: Problem with GetModuleFileName()
Post by: TimoVJL on January 31, 2011, 11:13:59 AM
WinCE 5 use WinCE subfolder not Win folder.
Title: Re: Problem with GetModuleFileName()
Post by: CommonTater 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...
Title: Re: Problem with GetModuleFileName()
Post by: CommonTater 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)
Title: Re: Problem with GetModuleFileName()
Post by: TimoVJL 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 ?

Title: Re: Problem with GetModuleFileName()
Post by: CommonTater 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?

Title: Re: Problem with GetModuleFileName()
Post by: pitter2206 on January 31, 2011, 11:30:38 AM
The folder CE is there, but there is no shlwapi.h in it...
Title: Re: Problem with GetModuleFileName()
Post by: CommonTater on January 31, 2011, 11:35:33 AM
The folder CE is there, but there is no shlwapi.h in it...

Not the CE folder... it should be in the include\win folder which your system should also search...  
Title: Re: Problem with GetModuleFileName()
Post by: Stefan Pendl on January 31, 2011, 11:46:25 AM
The shell lightweight utility API uses the Shlwapi.dll, if this DLL is present on your pocket PC system, then you would only need to copy the Shlwapi.h header from Win to WinCE, but you would need to compile the Shlwapi.lib for pocket PC or include a .DEF file.

We would need to know the reason, why Pelle did not include it for PPC.



A moderator should move this to the Pocket PC board, I think.
Title: Re: Problem with GetModuleFileName()
Post by: pitter2206 on February 01, 2011, 11:57:41 AM
Hi Stefan,

I was able to move it myself... ;)