64-bit opendir and readdir in C++Builder - c++

I'm using the VCL tools in C++Builder 11 for Windows desktop development. I am trying to get the C functions opendir and readdir to work in a 64-bit app. I have read the IBM website (link below) about the 64-bit version of opendir and readdir but I can't configure my code to work with the 64-bit versions. The code below shows a single button app with code that reads and displays the name of each file in a folder. This works as a 32-bit app. On the 64-bit platform this code fails in the while loop calling readdir. Can you show how to adjust this code so it works on the 64-bit VCL platform in C++Builder.
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <dirent.h>
#include <System.SysUtils.hpp>
//-----------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//----------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DIR *pDir;
struct dirent *dirU;
pDir = opendir( "C:\\test\\" );
if (pDir == NULL) {
ShowMessage("error");
}else{
int count = 1;
while ((dirU = readdir(pDir)) != NULL) //fails here
{
ShowMessage(dirU->d_name);
count++;
}
}
}
//------------------------
IBM 64 bit opendir and readdir

I'm at a loss why you are looking at the IBM site, feels like a flashback of a long long time ago. Anyway, why not use native functions like GetFiles? See https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.IOUtils.TDirectory.GetFiles

Related

How to find the "Saved Games" folder programmatically in C/C++?

I am writing a game. I am planning to store saves in the "saved games" directory.
How to find the location of the Saved Games folder programmatically?
It needs to work on non-English Windows. Hacks like %USERPROFILE%\Saved Games are not an option.
The Saved Games directory can be located with the SHGetKnownFolderPath() function, available since Windows Vista and Windows Server 2008.
Note that the FOLDERID_SavedGames argument is a C++ reference. Replace with &FOLDERID_SavedGames to call from C code.
Tested successfully on the first online MSVC compiler I could find:
https://rextester.com/l/cpp_online_compiler_visual
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#include <stdio.h>
#include <shlobj.h>
#include <objbase.h>
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "ole32.lib")
int main(void)
{
PWSTR path = NULL;
HRESULT r;
r = SHGetKnownFolderPath(FOLDERID_SavedGames, KF_FLAG_CREATE, NULL, &path);
if (path != NULL)
{
printf("%ls", path);
CoTaskMemFree(path);
}
return 0;
}

Borland C++ Builder LoadFromResourceID Results in EAccess Violation

I'm using Borland C++Builder 5 to write a game program. I'm trying to load a bitmap from a resource file I created. I cannot get the bitmap to load from
the resource file either by ID or by Name. It will load from file, but I want
to use the resource file. Loading from ID or Name results in the following error message at runtime:
Project HoldemProbs.exe raised exception class EAcessViolation with message
'Access violation at address 0043F66E in module 'HoldemProbs.exe'.
Read of address 000003EB. Process stopped. Use Step or Run to continue.
I'm pretty new to C++ and C++Builder, and I am sure I am doing something wrong. I just can't figure out what. I think it probably has something to do with the HInstance value, but I don't know what.
Below are relevant pieces of code:
ResRC.h
#ifndef RESRC_H
#define RESRC_H
#define RC_REDBACK 1000
#endif
RedBack.rc
RC_REDBACK BITMAP "<MYSOURCEPATH>\RedBack.bmp"
HoldemProbs.cpp
#include <vcl.h>
#pragma hdrstop
USERES("HoldemProbs.res");
USEFORM("..\Source\HoldemCalc.cpp", Form1);
USERES("..\Source\RedBack.res");
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->Title = "HoldemEval";
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
HoldemCalc.cpp
#include <vcl.h>
#include <cstring.h>
#include <HoldemEval.hpp>
#include <Graphics.hpp>
#pragma hdrstop
#pragma resource "*.dfm"
#include <Graphics.hpp>
#include <ResRC.h>
#include "HoldemCalc.h"
#include <string>
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CardBackTst(); //Run test of card back bitmap useage
}
CardBackTst.cpp
void CardBackTst(void){
Graphics::TBitmap* RedBack = new Graphics::TBitmap;
Form1 -> Debug -> Text = DEBUGTEXT; //See below for DEBUGTEXT
RedBack -> LoadFromResourceID ((ARG1),RC_REDBACK); //See below for ARG1
}
There is an Edit Box called Debug used to display debug information. At various times, DEBUGTEXT has been (w/o surrounding "s):
"(int)(GetModuleHandle(NULL)"
"reinterpret_cast<int>(HInstance)"
"(int)HInstance"
"(int)(GetModuleHandle("<MYFINALPATH>\\HoldemProbs.exe"))" // ANSI string
"RC_REDBACK" (This yielded the correct value -- 1000)
"reinterpret_cast<int>(RedBack -> Handle)"
"(int)(Form1 -> Handle)"
At various times, ARG1 has been each of the above values of DEBUGTEXT, except for RC_REDBACK. All have resulted in an EAccessViolation error.
The following statement to load from the resource name has been used
with each ARG1 value, with the same EAcessViolation result:
RedBack -> LoadFromResourceName ((ARG1),"MYPATH\\RedBack.bmp");
However, the following LoadFromFile() statement to load from the actual bitmap file does work:
RedBack -> LoadFromFile("MYPATH\\RedBack.bmp");
I see two problems with this code:
RedBack.rc does not have a #include "ResRC.h" statement:
#include "ResRC.h" // <-- add this!
RC_REDBACK BITMAP "<MYSOURCEPATH>\RedBack.bmp"
Without that, RC_REDBACK will not be #defined, and so the bitmap resource will be identified by the literal name "RC_REDBACK" and not by the ID number 1000. As such, you will have to use LoadFromResouceName() instead of LoadFromResourceID():
RedBack->LoadFromResourceName((unsigned)HInstance, "RC_REDBACK");
Use a resource viewer tool to verify how your resources are actually identified in the final executable.
inside of CardBackTst(), the global Form1 pointer may not have been assigned yet, since the TForm1 object is still being constructed. CardBackTst() should not be relying on the global pointer at all. It should take the Form pointer as an input parameter instead:
//#include "ResRC.h"
void CardBackTst(TForm1 *Form) {
Graphics::TBitmap* RedBack = new Graphics::TBitmap;
if (Form) Form->Debug->Text = DEBUGTEXT;
//RedBack->LoadFromResourceID((unsigned)HInstance, RC_REDBACK);
RedBack->LoadFromResourceName((unsigned)HInstance, "RC_REDBACK");
}
...
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CardBackTst(this);
}
A better solution would be to pass in the TEdit directly instead of the TForm itself:
//#include "ResRC.h"
void CardBackTst(TEdit *Debug) {
Graphics::TBitmap* RedBack = new Graphics::TBitmap;
if (Debug) Debug->Text = DEBUGTEXT;
//RedBack->LoadFromResourceID((unsigned)HInstance, RC_REDBACK);
RedBack->LoadFromResourceName((unsigned)HInstance, "RC_REDBACK");
}
...
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CardBackTst(Debug);
}
Or, use a callback function instead:
//#include "ResRC.h"
typedef void __fastcall (__closure *TCardDebugProc)(const String &);
void CardBackTst(TCardDebugProc DebugProc) {
Graphics::TBitmap* RedBack = new Graphics::TBitmap;
if (DebugProc) DebugProc(DEBUGTEXT);
//RedBack->LoadFromResourceID((unsigned)HInstance, RC_REDBACK);
RedBack->LoadFromResourceName((unsigned)HInstance, "RC_REDBACK");
}
...
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CardBackTst(&LogDebug);
}
void __fastcall TForm1::LogDebug(const String &DebugText)
{
Debug->Text = DebugText;
}

c++ Export project to exe

Im using Visual Studio 2015 and want to export my project which only includes the .cpp file and a .wav file which supposed to be played. How do i export the exe including the .wav file
#include <Windows.h>
#include <conio.h>
#include <iostream>
using namespace std;
#include <stdio.h>
# include <winuser.h>
#pragma comment(lib, "winmm.lib")
int main()
{
FreeConsole();
while (1)
{
for (char button = 0; button < 256; button++)
{
if (GetAsyncKeyState(button) & 0x8000)
{
PlaySound(TEXT("dab.wav"), NULL, SND_FILENAME);
}
}
Sleep(5);
}
}
You can add your .wav file as resource of your project.
see documentation here: https://msdn.microsoft.com/en-us/library/8fc1e5by.aspx
Then you can play your sound from resource id.
see documentation here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd743679(v=vs.85).aspx

Making directory with vitual studio 2012 coding in c++

Good evening everyone, please I'm writing a Library management application in c++ using virtual studio 2012. I had already writing some codes using Dev c++ it worked but when I switched to visual studio it gives error. It involves creating folders and checking if the folders were actually created. That is using dir and mkdir.
Windows and Linux (POSIX) don't support the same API for most file system functions. You can use Microsoft's platform-specific APIs like CreateDirectory() or use the POSIX-like versions like _mkdir().
If you have a more recent C++ compiler / standard library, you can use the experimental filesystem library that is slated to become part of standard C++, perhaps as early as C++17. If not, you can use Boost.Filesystem from which the pre-standard experimental library was drawn.
Here's a complete, minimal example using Boost.Filesystem, which will work on both Windows and Linux without modification:
#include <iostream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main()
{
if( !fs::exists( "my_dir" ) )
{
if( fs::create_directory( "my_dir" ) )
{
std::cout << "Directory created!\n";
}
}
}
See it run: Coliru.
Here's the same code but with std::experimental::filesystem: Coliru.
You would need the appropriate include and linker paths setup in your build system for either of these to work locally. The biggest "gotcha" using the filesystem is that it throws exceptions for a lot of errors by default. You can either setup try/catch blocks at the appropriate places or pass in an error code param to make it return the status there instead.
#include <stdio.h>
#include <windows.h>
int main() {
if (!CreateDirectoryA("C:\\ABC123", NULL))
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
printf("Already Exists");
}else if (GetLastError()== ERROR_PATH_NOT_FOUND)
{
printf("Path not found");
}
}else{
printf("Created..");
}
}
simple function will do.
Thanks alot guys but I found this that solved my problem
#include <iostream>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void main( void )
{
if( _mkdir( "\\testtmp" ) == 0 )
{
printf( "Directory '\\testtmp' was successfully created\n" );
system( "dir \\testtmp" );
if( _rmdir( "\\testtmp" ) == 0 )
printf( "Directory '\\testtmp' was successfully removed\n" );
else
printf( "Problem removing directory '\\testtmp'\n" );
}
else
printf( "Problem creating directory '\\testtmp'\n" );
int a;
cin >> a;
}
the cin >> a; is just to keep the output screen so I can see the result

embedding python in c++

I created a VCL Application in c++, borland. In my project there is a file where I have implemented embedded python in the methods defined in the same(my application contains a button which calls the method in which embedded python is implemented). when I compile, my build is successful. but when I run my application, and click on the button it shows the run time error : "Access violation at address 1E091375 in module 'PYTHON25.DLL'. Read of address 00000004" . please help.
I have never used Python before.
my program:
#pragma hdrstop
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Python.h"
#include "Unit1.h"
#include "Unit2.h"
#pragma link "python25_bcpp.lib"
//---------------------------------------------------------------------------
#pragma package(smart_init)
bool callHelloWorld(int intVal)
{
char fName[] = "Hello"; //file name
char cFunc[] = "hello"; //method name
char *pfName, *pcFunc;
PyObject *pName, *pModule, *pDict, *pFunc ;
pfName = fName;
pcFunc = cFunc;
Py_Initialize();
pName = PyString_FromString(pfName);
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, pcFunc);
if (PyCallable_Check(pFunc))
{
PyObject_CallObject(pFunc, NULL);
} else
{
PyErr_Print();
}
// Py_DECREF(pModule);
// Py_DECREF(pName);
Py_Finalize();
return 0;
}
Check the return values of PyImport_Import (is the module in the search path?) and PyDict_GetItemString.
If that doesn't help put some trace messages in your app to see where it crashes.
This works for me:
Just delete Py_Finalize()
I read in another site that Py_Finalize has some problems in specific cases such as threading.