flat assembler
Message board for the users of flat assembler.

Index > Windows > File name

Author
Thread Post new topic Reply to topic
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
I do very little win32 programming, but have the need to write a simple win32 program in fasm.
What i need to know, is what is the best way to save a file with a name + number

e.g. say i am saving screenshots, but do not want to over write the one before.

So the first would be shot1.jpg, shot2.jpg, shot3.jpg etc.

So if i wanted to call one after the other the first part of the name 'shot' + consecutive numbers.

I will be testing for a file with that name, then if found, increasing the number until a new file name is found, then using that name for the next screenshot.
Thanks for any help.
Post 21 Jan 2010, 05:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17665
Location: In your JS exploiting you and your system
revolution
Just use CreateFile with the CREATE_NEW flag set. You get and error indicating if the file already exists. Then just increment your number and try again.
Post 21 Jan 2010, 05:24
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Hi Dex, this demo show basics of Win32 file handling...
http://board.flatassembler.net/download.php?id=4624
Post 21 Jan 2010, 05:48
View user's profile Send private message Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 709
Location: Adelaide
sinsi
If you don't care what it is called you could try GetTempFileName.
You could also try wsprintf using 'shot%u' and a 4 digit number you track, modifying the %u (sorry. too lazy to look up leading zeros)
Another is to increment the last char of the name from '0' to '9', roll it over into the previous char and so on.
Post 21 Jan 2010, 06:09
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Thanks everyone, it was very easy in the end.
Post 22 Jan 2010, 18:13
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
As a side note to this topic, the app i made was a screenshot app, that hooked into a keypress and took a full screenshot and also played a camera clicking sound.
Saving each image as shot??.bmp ?? being a number, increasing each shot.
It works fine in XP, but under windows7 it would only take 6 shots before not working.

The problem seems to be in the key hook, its seem windows only lets 6 key hooks per app, it maybe a security thing ?.
Post 24 Jan 2010, 21:08
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
I wrote the same thing a while back
but instead of hooking the keyboard i sent
input event and copied it via clipboard.
If you would like to C my code just ask...
Post 24 Jan 2010, 23:13
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Dex4u wrote:
The problem seems to be in the key hook, its seem windows only lets 6 key hooks per app, it maybe a security thing ?.
Did it repeatedly only trigger your hotkey 6 times, or is it perhaps just failing to trigger the hotkey when an elevated app has focus? Which hotkey did you choose? (Win+xx can be problematic).

Other apps have no problems hotkey-triggering under Win7 (I use several Smile), so it's bound to be a quirk with either your code or your system...

revolution wrote:
Just use CreateFile with the CREATE_NEW flag set. You get and error indicating if the file already exists. Then just increment your number and try again.
Very good piece of advice - other people often generate a new filename string, test whether the file exists, and if not they proceed to creating the file... this is open to race condition bugs, whereas your solution atomically fails or succeeds.

_________________
Image - carpe noctem
Post 25 Jan 2010, 00:59
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Thanks bitshifter, i would like to see your code.

f0dder, I use F9, the hook is a background hook so the program need no focus.
It works normal until and takes screenshots on pressing the F9 key, but once you have pressd the key 6 times, it does not take any more shots until you close the screen shot app and re-open it.

When you say other win7 work OK, thats maybe because they use the .net.
Do you know of a asm example of a keyhook, that works more than 6 in the background, ?.
Post 25 Jan 2010, 06:15
View user's profile Send private message Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 709
Location: Adelaide
sinsi
>Thanks everyone, it was very easy in the end
So tell us (me) how you did it?

As for your problem, post some code - I have win7
Post 25 Jan 2010, 06:36
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Dex4u wrote:
f0dder, I use F9, the hook is a background hook so the program need no focus.
Well, if you're using a windows hook, that's going to cause DLLs to be injected into processes - those won't be injected to elevated apps when you're running as LUA. That might have something to say, but it's just a guess.

Dex4u wrote:
It works normal until and takes screenshots on pressing the F9 key, but once you have pressd the key 6 times, it does not take any more shots until you close the screen shot app and re-open it.
Sounds weird if it always happens after exactly 6 shots. Can you duplicate the problem with a minimal amount of code? (ie, F9 hook and MessageBox, or something similar).

Dex4u wrote:
When you say other win7 work OK, thats maybe because they use the .net.
Nope - it's Screenshot Captor and FARR from DonationCoder I've tested. They're written in Borland C++ Builder which isn't assembly, but it's definitely native code and not dotNET.

_________________
Image - carpe noctem
Post 25 Jan 2010, 07:10
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Ok, here is my code for taking screenshot...
Code:
#include <windows.h>
#include <stdio.h>

void TakeScreenShot(LPCSTR);

int main(int argc, char **argv)
{
   TakeScreenShot("ScreenShot.bmp");
   return 0;
}

void TakeScreenShot(LPCSTR lpszFileName)
{
   keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
   keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
   
   OpenClipboard(NULL);
   HBITMAP h = (HBITMAP)GetClipboardData(CF_BITMAP);
   CloseClipboard();

   BITMAPINFO bmpInfo;
   ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
   bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

   HDC hdc = GetDC(NULL);
   GetDIBits(hdc, h, 0, 0, NULL, &bmpInfo, DIB_RGB_COLORS);

   if(bmpInfo.bmiHeader.biSizeImage <= 0)
   {
      bmpInfo.bmiHeader.biSizeImage =
         bmpInfo.bmiHeader.biWidth *
         abs(bmpInfo.bmiHeader.biHeight) *
         (bmpInfo.bmiHeader.biBitCount + 7) / 8;
   }

   LPVOID pBuf = malloc(bmpInfo.bmiHeader.biSizeImage);

   bmpInfo.bmiHeader.biCompression = BI_RGB;
   GetDIBits(hdc, h, 0, bmpInfo.bmiHeader.biHeight, pBuf, &bmpInfo, DIB_RGB_COLORS);

   BITMAPFILEHEADER bmpFileHeader;
   bmpFileHeader.bfReserved1 = 0;
   bmpFileHeader.bfReserved2 = 0;
   bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bmpInfo.bmiHeader.biSizeImage;
   bmpFileHeader.bfType = 0x4D42; // BM (little endian = MB)
   bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

   FILE* fp = fopen(lpszFileName, "wb");
   fwrite(&bmpFileHeader, sizeof(BITMAPFILEHEADER), 1, fp);
   fwrite(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER), 1, fp);
   fwrite(pBuf, bmpInfo.bmiHeader.biSizeImage, 1, fp);

   ReleaseDC(NULL, hdc);
   free(pBuf);
   fclose(fp);
}
    

Enjoy Smile

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 25 Jan 2010, 09:04
View user's profile Send private message Reply with quote
score_under



Joined: 27 Aug 2009
Posts: 27
score_under
@Bitshifter: that was horrible. (relying on windows to send it to the clipboard then retreiving from the clipboard)
I believe there's some method that involves opening the desktop window's DC and collecting the image from there.
Post 26 Jan 2010, 12:07
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.