Reading a Buffer

Cole

Newbie
Joined
Jan 19, 2004
Messages
6,427
Reaction score
1
This has nothing to do with HL2, but I havn't been used C++ in a very long time and I got a dll that basically opens a Starcraft MPQ file and then opens the chk file and write it all to a buffer.

I was wondering how to exactly read this information.

LPVOID lpBuffer; - The Buffer
lpBuffer = new BYTE[SFileGetFileSize(hFile, NULL)]; - Setting the buffers size to the proper size.

Currently I just write it to a file, but I need to get this data to a C# application. I've got C++ dll and C# working in the same sanbox. Now I'd rather not write it to a file and read it all in C++ and then send my own data to my C# application as a string or integer.

I'm just asking for a simple function to read it.
 
I'm not asking how to load the dll's I've got that all working fine.

What i'm asking has to do purely with C++. Basically how to read my lpBuffer.
 
You are going to have to be more specific.

Are you asking how to do file i/o in C++ or how to access the data in C++ from a C# app? If it is the latter, then you want a bridge to marshal the data from an unmanaged environment. You cannot just read lpBuffer from C#. It needs to be marshalled into the managed environment. The .Net interop provides that mechanism. i.e. From within C#, you call a C function that returns the data in lpBuffer.

Here is an example of getting a string from an unmanaged dll:
http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
 
I'm asking how to read a buffer in C++.
I made a buffer: LPVOID lpBuffer;
I set its size: lpBuffer = new BYTE[SFileGetFileSize(hFile, NULL)];
I pass info from a file into it = SFileReadFile(hFile, lpBuffer, nNumberOfBytesToRead, &lpNumberOfBytesRead, NULL)

Now I would prefer to be able to read this in C++ and then pass a completely seperate string onto C#. My problem is reading this in C++.

I basically want to read it byte by byte in C++ and then use the information to return a string onto C#. I only need help on reading it in C++.

Now I've seen countless ways to get this into memory(which is what I've done above and easily did), but not read it once it's there.
 
Okay, that is a little clearer. I still don't know what you are trying to do or what data you are working with, so I can't really help you very much. In any case, you read the buffer as you would read any other data. If the data is just text, then you could just read it in as text. For instance:
Code:
char* lpBuffer = new char[SFileGetFileSize(hFile, NULL) + 1];
SFileReadFile(hFile, lpBuffer, nNumberOfBytesToRead, &lpNumberOfBytesRead, NULL);
// might need to tack a \0 on to the end
That's it. Again, that is assuming that you have read in the desired file within the archieve and what you are reading is just text.
 
Ok I've tracked down the problem a little better. The C# and C++ dll are talking to eachother perfectly.

I wrote some code to read the file in C#. Now using ReadToEnd, it only read until whitespaces. Then I used a while statement and just read each character until the end of the block and I got the full file. Then I modified it to read everything as byte. Now in C# I pulled this off in just a few minutes.

C++ would always return up until the whitespaces to. So is there anyway to read a string character by character in C++ as a byte?
 
For your info, a char in c++ is a single byte. It's not because it's a char datatype that you're forced to work with chars.

You'll have to work with unsigned char though.

For example this will read 85 bytes from a file
Code:
typedef unsigned char byte;

byte bytes[85];
ifstream inputStream;

inputStream.open(pathtofile,std::ios::in |std::ios::binary);
inputStream.read((char *)bytes,sizeof(bytes));
 
But i'm not opening a file in C++, well I am using diffrent functions to get the file from an archive. Then this function puts everything into a buffer.

Now when I read this buffer out into the console it will only read the first 5 text characters. This is a major problem because it is stopping at the white spaces and I need it outputted as numbers.

Now I could do this in C# but then I have to have C++ write the file(Already Done) and then read it in C#. I would prefer to just send the data from C++ to C# using a dll. Now I don't need help with the dll part I have that fully working.
 
Maybe you should just create a struct for your fileformat and cast your buffer to it.

Code:
struct Section
{
   char name[4];
   long length;
   byte data;   //of length
};

Section * sect = (Section *) lpBuffer;
//here you can whatever you want with the name and length
//to get at the data:
unsigned char *data = &sect->data;

for(int i = 0;i<length;i++)
{
   int b = data[i];
}

To get the next section, you'll need to add the size of the previous section to your lpBuffer:

(lpBuffer + sect->length + 8) would be the address of the next one.
 
Back
Top