auxDIBImageLoad returns null - opengl

Hi i am doing a project with opengl
What I'm stuck with is that the method 'auxDIBImageLoad' always returns null even if the file exists.
My code is below:
AUX_RGBImageRec *LoadBMPFile(char *filename)
{
FILE *hFile = NULL;
if (!filename) return NULL;
hFile = fopen(filename, "r");
if (hFile)
{
printf("file opened\n");
fclose(hFile);
if (!auxDIBImageLoad(filename)) {
printf("but file is null\n");
}
return auxDIBImageLoad( filename );
}
printf("file open failed\n");
return NULL;
}
Then the log is below:
file opened
but file is null
load bmp failed
I already searched for fixing it so that I already set to Multibyte char set.
But it doesn't work after setting that.
Please help me.

Related

How to open file name contain special characters in FFmpeg?

my project process the video by FFmpeg libarary, I test the my code with file which the file name contains special characters(like '()+,-.09#A[]^_`a{}~®,Ѐ,ऄ, ఆ, Ⴀ,ᐁ,ᣀ,ᴀ,₠,⓪,⭙,ⶀ,㈀,啊.mp4'), but it failed to open the video stream at avformat_open_input().
I test it via ffmpeg.exe with command line, it can open the file successfully.
This is my code snippet:
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVFrame *pFrameRGB = NULL;
unsigned int i, videoStream;
AVCodec *pCodec;
AVFrame *pFrame;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer;
AVPixelFormat ImgFmt = AV_PIX_FMT_YUV420P;
// Register all formats and codecs
av_register_all();
// Open video file
if (avformat_open_input(&pFormatCtx, inputFilePath, NULL, NULL) != 0)
{
printf("Couldn't open file.");
return -1;
}
it occur error at the avformat_open_input(), I think it failed because of the Unicode file name string
Anybody can help me?
Thanks

Unable to Read from File while using ReadFIle() function in C++

I am facing some issues while reading data from file using ReadFile() function of C++ (Microsoft specific probably).
Here is my code
Write On File
void ClientA::SharePublicKey()
{
printf("Sharing Public Key\n");
HANDLE hFile = NULL;
hFile = CreateFile(TEXT("D:\\My_Proj\\shared\\PublicKeyB.txt"), // name of the write
GENERIC_WRITE, // open for writing
FILE_SHARE_WRITE, // do not share
NULL, // default security
CREATE_NEW, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
//DisplayError(TEXT("CreateFile"));
//_tprintf(TEXT("Terminal failure: Unable to open file \"%s\" for write.\n"), argv[1]);
return;
}
// _tprintf(TEXT("Writing %d bytes to %s.\n"), dwBytesToWrite, argv[1]);
bool bErrorFlag = WriteFile(
hFile, // open file handle
pbPublicKey, // start of data to write
dwPublicKeyLen, // number of bytes to write
&lpNumberOfBytesWritten, // number of bytes that were written
NULL); // no overlapped structure
if (FALSE == bErrorFlag)
{
// DisplayError(TEXT("WriteFile"));
printf("Terminal failure: Unable to write to file.\n");
return;
}
else
{
if (lpNumberOfBytesWritten != dwPublicKeyLen)
{
// This is an error because a synchronous write that results in
// success (WriteFile returns TRUE) should write all data as
// requested. This would not necessarily be the case for
// asynchronous writes.
printf("Error: dwBytesWritten != dwBytesToWrite\n");
}
else
{
// _tprintf(TEXT("Wrote %d bytes to %s successfully.\n"), dwBytesWritten, argv[1]);
}
}
CloseHandle(hFile);
}
Read That File
void ClientA::ReadPublicKeyOfOtherPeer()
{
HANDLE hFile = NULL;
DWORD dwBytesRead = 0;
BYTE* ReadBuffer = NULL;
OVERLAPPED ol = {0};
hFile = CreateFile(TEXT("D:\\My_Proj\\shared\\PublicKeyB.txt"), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
NULL // no attr. template
);
if (hFile == INVALID_HANDLE_VALUE)
{
_tprintf(TEXT("CreateFile\n"));
_tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"));
printf("Error %x\n", GetLastError());
return;
}
if( FALSE == ReadFile(hFile, ReadBuffer, dwPublicKeyLen, &lpNumberOfBytesWritten, &ol) )
{
// DisplayError(TEXT("ReadFile"));
printf("Terminal failure: Unable to read from file.\n GetLastError=%08x\n", GetLastError());
CloseHandle(hFile);
return;
}
if (dwBytesRead > 0 && dwBytesRead <= dwPublicKeyLen-1)
{
ReadBuffer[dwBytesRead]='\0'; // NULL character
//_tprintf(TEXT("Data read from %s (%d bytes): \n"), argv[1], dwBytesRead);
printf("%s\n", ReadBuffer);
}
else if (dwBytesRead == 0)
{
//_tprintf(TEXT("No data read from file %s\n"), argv[1]);
}
else
{
// printf("\n ** Unexpected value for dwBytesRead ** \n");
}
retrievedPublicByteArray = ReadBuffer;
CloseHandle(hFile);
}
By SharePublicKey method I am saving the data in a file. And I have checked that it successfully saves data on the file and the data on the files are seems to be valid.
And by ReadPublicKeyOfOtherPeer method I am reading the file which was previously saved. But reading is not successful In out put I found the following line -
Terminal failure: Unable to read from file.
GetLastError=000003e6
You are passing uninitialized pointer ReadBuffer to ReadFile. You need a buffer that is large enough to receive the results.

cannot write binary data in a file using c

I am using vs2010 in an windows vista os the problem is that i want to read and exe file encrypt it and store it but when i write the data back nothing gets inserted i mean the file is created but no errors and the file is empty and i have done it a thousand times and it works on eclipse but not on vs2010 and i need to port it into a gui can anyone please redirect me where i am wrong
FILE *pFile, *file;
size_t result;
pFile = fopen(fName, "r+b");
if (pFile==NULL) {MessageBox(NULL, L"Could not open file", L"Information", MB_ICONERROR); return FALSE;}
fseek(pFile, 0 ,SEEK_END);
sData->fSize = ftell(pFile);
rewind(pFile);
sData->fbuffer = (unsigned char *) malloc(sizeof(char) * sData->fSize);
if (sData->fbuffer == NULL) {MessageBox(NULL, L"Memory error", L"Information", MB_ICONERROR); fclose(pFile); return FALSE;}
file = fopen("out.txt", "w+b");
while ((result = fread(sData->fbuffer, 1, sData->fSize, file)) > 0) {
if (!(fwrite(sData->fbuffer, 1, result, file))) {
MessageBox(NULL, L"Write error", L"Information", MB_ICONERROR);
}
fclose(file);
}
fclose(file);
//result = fread(sData->fbuffer, 1, sData->fSize, pFile);
//if (result != sData->fSize) {MessageBox(NULL, L"Read error", L"Information", MB_ICONERROR); fclose(pFile); return FALSE;}
fclose (pFile);
return TRUE;
EDIT
I am really sorry the problem is with the location of the file it is in unicode format where as fopen accepts ascii and the location only shows C I have to convert it to get the proper result thanks
You would want to use pFile instead of file in fread().
//---------------------------------------------------- pFile not file
while ((result = fread(sData->fbuffer, 1, sData->fSize, pFile)) > 0) {
if (!(fwrite(sData->fbuffer, 1, result, file))) {
MessageBox(NULL, L"Write error", L"Information", MB_ICONERROR);
}
fclose(file);
}
I think the file that you are opening for read is not Ascii, so you assumption of calculation of file size based on size of char is wrong.
if you calculate right size of file in bytes and read and write those many bytes you should be fine.
You are reading and writing to/from the same file.
while ((result = fread(sData->fbuffer, 1, sData->fSize, file)) > 0) {
if (!(fwrite(sData->fbuffer, 1, result, file))) {
Since you just opened this file it reads nothing, so nothing is written as well.
From the man page:
w+ or wb+ or w+b
Truncate to zero length or create file for update.

How make FILE* from HANDLE in WinApi?

Is there easy way to create FILE* from WinApi HANDLE which points to one end of pipe?
Something like we do in unix: fdopen(fd,<mode>);
You can do this but you have to do it in two steps. First, call _open_osfhandle() to get a C run-time file descriptor from a Win32 HANDLE value, then call _fdopen() to get a FILE* object from the file descriptor.
FILE* getReadBinaryFile(LPCWSTR path) {
HANDLE hFile = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
return nullptr;
}
int nHandle = _open_osfhandle((long)hFile, _O_RDONLY);
if (nHandle == -1) {
::CloseHandle(hFile); //case 1
return nullptr;
}
FILE* fp = _fdopen(nHandle, "rb");
if (!fp) {
::CloseHandle(hFile); //case 2
}
return fp;
}
my code for get an open read binary file descriptor.
you should use fclose to close FILE* if you don't need it .
i didn't test for case 1 and 2, so use it at your own risk.
you can't exchange(convert) them.. if you need to have a file with FILE* and HANDLE you need to open it twice

WinINet trouble downloading file to client?

I'm curious why I'm having trouble with this function. I'm downloading a PNG file on the web to a destination path. For example, downloading the Google image to the C: drive:
netDownloadData("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png", "c:\file.png");
The file size is correct after downloading. Nothing returning false. When I try opening it it won't show the image. Any ideas are helpful. Thanks!
Here's the code:
bool netDownloadData(const char *strSourceUrl, const char *strDestPath)
{
HINTERNET hINet = NULL;
HINTERNET hFile = NULL;
char buffer[1024];
DWORD dwRead;
String sTemp;
FILE *fp = NULL;
DWORD size = 0;
// Open a new internet session
hINet = netInit();
if (hINet == NULL) {
sprintf(buffer, "Initializing WinINet failed.", strSourceUrl);
utilLog(buffer);
netCloseHandle(hINet);
return false;
}
// Open the requested url.
hFile = netOpenUrl(hINet, strSourceUrl);
if (hFile == NULL) {
sprintf(buffer, "URL failed upon loading: %s\n", strSourceUrl);
utilLog(buffer);
netCloseHandle(hINet);
return false;
}
// Read file.
while (InternetReadFile(hFile, buffer, 1023, &dwRead))
{
if (dwRead == 0)
break;
buffer[dwRead] = 0;
sTemp += buffer;
size += dwRead;
}
// Load information to file.
fp = fopen(strDestPath, "wb");
if (fp == NULL)
return false;
fwrite(sTemp, size, 1, fp);
fclose(fp);
InternetCloseHandle(hFile);
InternetCloseHandle(hINet);
return true;
}
What data type is String? Avoid storing binary data in strings because NULLs in the data can potentially cause problems. Just write the buffer as and when you read it:
// Load information to file.
fp = fopen(strDestPath, "wb");
if (fp == NULL)
return false;
// Read file.
while (InternetReadFile(hFile, buffer, 1024, &dwRead))
{
if (dwRead == 0)
break;
fwrite(buffer, dwRead, 1, fp);
}
fclose(fp);
It looks like your second and third args to fwrite are transposed. See fwrite docs for explanation.
try:
fwrite(sTemp, 1, size, fp);