|
The WriteFile doesn't need or require an overlapped access and I would be intrigued what security attributes you are feeding in via sa. Naming the event is fine but why are you needing to provide specific security attributes are you doing something special?
The worry I gleaned was you hold the event handle in sysblk.mail are you remembering to copy the handle reference into your overlapped structure? It's sort of odd what you are doing there but not wrong so long as you remember to transfer the handle.
The usual read thread code direct from MSDN looks like
char buffer[100];
OVERLAPPED ovlp = {0};
ovlp.hEvent = CreateEvent(NULL, false, false, NULL);
if (ovlp.hEvent == NULL) {
}
DWORD read;
do {
ReadFile(mailslot, buffer, sizeof(buffer), &read, &ovlp);
buffer[read] = 0;
WaitForSingleObject(ovlp.hEvent, INFINITE);
process_message(buffer);
} while (strcmp(buffer, "exit"));
The write thread is create function is non overlapped
HANDLE mailslot = CreateFile("\\\\.\\mailslot\\myslot",
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
0,
NULL);
They are the by the book MSDN examples and your code should be some extension of that process inside your sysblk code.
Also remember this sort of thing is for relatively low speed, small packet exchanges if you want to go beyond that you need to change to memory mapped files.
In vino veritas
modified 5-Apr-16 23:21pm.
|
|
|
|
|
Hello Sir,
Can anybody help me how to customize the ribbon bar means adding ribbon category at run time similar to the QAT custmization
Any sample code or reference is much appreciated
Thanks,
Ashish
asdsa
|
|
|
|
|
|
Hi,
Does anyone have a program written in C++ that would allow input of C type syntax e.g. if(x>0 && x<9), parse this statement and produce a node tree.
I have being doing some research and have code written to read in the statement and split each item, but from here I am lost. I think I am supposed to use a LALR analyser and produce a syntax tree.
Any help appreciated from a stressed out woman!
|
|
|
|
|
Sorry but this site does not provide code to order. You need to be more specific about the help you need.
|
|
|
|
|
Hi all,
Good day.
Please am just learning application programming using the Win32 Api. l wrote a small program just like a crackme to save user key in a text file when key is correct and not to ask for the key next time the app is run. But should ask if the file is not found or the contents tampered. But it always ask for key every time the app is run.
l had been playing with CreateFile, ReadFile and WriteFile Functions but it doesn't work.
l had used OPEN_EXISTING as well as OPEN_ALWAYS in CreateFile function to Create the text file with user key but the app always ask for key even when the data file(.txt) exists.
l just need a guideline, proper API or sample code on how to achieve that. My code is a little big and not straight forward for me to put in here.
Thanks.
ADDENDUM/MODIFICATION
@Jeron1, @DavidCrow, @Jochen Arndt
Ok. Am confused here, and l don't know if am the one to be confused or the IDE or Microsoft themselves. Am using MS Visual studio 2010
BREAK DOWN.
I want it in a way that when the app is started, it should check if the keyfile exists. if exists, read the content and compare it with a keybuffer. if not it should ask for key.
code snippet:
.....
WM_CREATE:
keyFile=(L"C:\\key.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (keyFile != INVALID_HANDLE_VALUE){
if (GetLastError() == ERROR_ALREADY_EXISTS){
SetFilePointer(keyFile,0,0,FILE_BEGIN);
ReadFile(keyFile,szKeyRead,11,&dwBytes,NULL);
if(lstrcmp(szKeyRead,szKeyBuffer)==0){
}else{
DialogBox(hInst,MAKEINTRESOURCE(IDD_REG),hWnd,Reg);
<pre>
}
}else if(keyFile == INVALID_HANDLE_VALUE || keyFile==NULL|| keyFile==(HANDLE)ERROR_FILE_NOT_FOUND){
DialogBox(hInst,MAKEINTRESOURCE(IDD_REG),hWnd,Reg);
}else{
MessageBox(hWnd,L"Problem with Registration.\n",L"Registration",0);
}
CloseHandle(keyFile);
}</pre>
Quote: Now when l have the code above, the if() statements executes but lands at else if statement
which l think is wrong.
Quote: but to my surprise, when l change it to the code below, no part of the if statements runs and lasterror gives me 2.
keyFile=CreateFile(L"C:\key.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
When OK is clicked, the app manipulates user entered Name, character by character and store it in a static TCHAR szKeyBuffer[12];
<blockquote class="quote"><div class="op">Quote:</div>
<pre>
if(lstrcmp(szKeyBuffer,szKey)==0){
keyFile=CreateFile(L"C:\key.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL); if(GetLastError() == ERROR_ALREADY_EXISTS){
MessageBox(hwnd,L"Sorry though!",L"Error",0);
SetFilePointer(keyFile,0,0,FILE_BEGIN);
WriteFile(keyFile,szKey,11,&dwBytes,NULL);
EndDialog(hwnd,TRUE);
}else
SetFilePointer(keyFile,0,0,FILE_BEGIN);
WriteFile(keyFile,szKey,11,&dwBytes,NULL);
EndDialog(hwnd,TRUE);
}else{
PostQuitMessage(0);
}</blockquote></pre>
So where is the fault from. Please l need a help/code skeleton on how to achieve my goal, which is when the app is started,it should check if keyfile(.txt file) exists and when exists, it should read the file content and check if the key is equal with the keystring the user entered, if equal app continues else it should ask for key.
Thanks.
modified 5-Apr-16 17:07pm.
|
|
|
|
|
Member 12139442 wrote: My code is a little big and not straight forward for me to put in here. Then create a small project that has a minimal amount of code that exhibits the problem, then try posting it here.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Member 12139442 wrote: But it always ask for key every time the app is run. Sounds like you are not (properly) checking for the file's existance.
Member 12139442 wrote: My code is a little big and not straight forward for me to put in here. So then what are you asking of us?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Open the file with OPEN_ALWAYS and check the error code to know if it has been created or an existing file has been opened:
SetLastError(0);
HANDLE hFile = CreateFile(fileName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
}
else
{
}
CloseHandle(hFile);
}
Alternatively check if the file exists and open it for reading or writing afterwards. This can be optimised by trying to open the file for reading and if that fails to open it for writing:
HANDLE hFile = CreateFile(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
}
else
{
hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
}
}
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
|
|
|
|
|
Hello,
Can somebody please give me an example of _tcslwr function used with BYTE array as input?
Thanks in advance.
|
|
|
|
|
This depends on the content stored in the byte array. Because the function expects a NULL terminated string, the byte array must contain such a string. You must also know the text encoding for the stored string.
Assuming your byte array contains an ASCII or ANSI string, you can call _strlwr :
_strlwr((char *)byteArray);
The casting to char * avoids compiler warnings or errors because BYTE is unsigned (unsigned char ).
If the resulting string must be printed out later with Unicode builds, convert it using the MultiByteToWideChar function (Windows)[^]. If it is ASCII or uses the current code page of the user running the application, it can be also converting by assigning the lowered string to a CString :
CString strLwr = _strlwr((char *)byteArray);
The above will work for Unicode and multi byte builds (with Unicode builds, the string is converted to Unicode).
|
|
|
|
|
Hi All , I have the below declaration in say abc.h and implementation of the member function in abc.cpp in Project A. How can I call/access some of this member functions in project B and Project C without including the A.lib in Project B and Project C?
As is I am getting linker error from project B and C
class __declspec(dllimport) B
{
public:
B();
static B* Ptr1();
static int fn1(D* ptr2);
D * fn2();
private:
static int Result;
static map <string, d*=""> map1;
};
class __declspec(dllimport) C
{
public:
C();
static C* Ptr1();
static int Fn4(const string& str1);
static int Fn6(const string &str3, B* ptr4);
static B* Fn7(string &str4);
static bool isBoolEnabled() { return Boolean;}
static void SetBoolean(bool bArg) {Boolean = bArg; }
private:
static map <string, b*=""> map3 ;
static map <int, string=""> map4;
static bool Boolean;
};
class __declspec(dllimport) D
{
vector <string> Token1;
vector <string> Token2;
vector <string> Token3;
}
|
|
|
|
|
Assuming that you are using Windows and that the code to be called is in a DLL, look at the LoadLibrary () and the GetProcAddress () APIs.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Thank you Daniel for the thoughts.. I am exploring other option due to the order I build the .lib gives linker error...if other option does not pan out I will go with your suggestion.. This was helpful..!
|
|
|
|
|
You have declared the classes as dllimport so you must add the .lib file to your linker section in your build.
|
|
|
|
|
Thank you Richard !.. The order I am compiling the different projects(.lib) cannot be changed. First project referencing member functions from other project that was not even compiled..so other than getprocaddress() , no better option ?
|
|
|
|
|
That makes no sense at all. Build the lib project first or waste your time.
|
|
|
|
|
I think he is trying to tell you he has got a circular dependancy problem.
In vino veritas
|
|
|
|
|
How to solve circular dependency in linker is shown here
Library order in static linking - Eli Bendersky's website[^]
The trick is to organize the library files as shown and link them twice which is perfectly valid.
I assume that is the reason you are trying to avoid including the library file.
In vino veritas
|
|
|
|
|
I need to open a file for both reading/writing using fstream and read each character then write that character back to the file. for example i have this code.
fstream in("test.txt",ios::in | ios::out);
if(!in)
cout<<"error...";
else
{
char ch;
in.seekg(0,ios::end);
int end=in.tellg();
in.seekg(0); for(int i=0;i<end;i++)
{
if(!in.get(ch).fail()) {
in.seekp(static_cast<int>(in.tellg())-1); if(in.put(ch).fail()) break; }
}
}
I have a file named "test.txt" which contains "ABCD". As i understand it both put() and get() methods of the stream object move the file pointer forward(i see that by getting the return value of tellg() or tellp() functions after each get() or put() method call). My question is when i comment out the code that will seek the stream pointer to "where it is now"(in.seekg(in.tellg()), the code will result incorrect results. I don't understand why this is since tellg() is showing the correct position of the character to be read next what is the purpose of explicitly seeking to it? The incorrect result is it write to the file "ABBB" instead of "ABCD". I am using visual studio 2005.
modified 3-Apr-16 16:26pm.
|
|
|
|
|
Read the documentation
The seekp method changes the location of a stream object's file pointer for output (put or write.) In most cases, seekp also changes the location of a stream object's file pointer for input (get or read.)
See that last bit of the statement. Your code assumes you are independently moving the get and put pointers. The implementation in linux depends also on stream type and I am not sure in VS2005 so read the documentation carefully.
In later versions of Visual Studio they use different terms
seekg => The absolute position in which to move the read pointer.
seekp => The position in the stream (meaning both read and write)
You also need a stream clear() after any seekp otherwise a seek to last character of a file will cause an error .. ATE flag gets set
In vino veritas
modified 3-Apr-16 13:36pm.
|
|
|
|
|
Thanks for your replay. But the code doesn't assume the input and output pointers are independently moved. In fact,in this case, the put() method will move the input pointer and the get() method move the output pointer as it is evident by looking at the values returned from tellp() and tellg(). This is why i am confused. if the put() statement in the code moves the input file pointer to the next character( which will be read by the next iteration) why do i need to seek to it explicitly is the question.
|
|
|
|
|
First you have the input and output pointers mixed in the above
Second the put function doesn't work the way you think in the above
MSDN => The member function put (which writes a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in that stream's intermediate buffer.
Put in.sync() after the put
MSDN => If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the program.
I think you need to do some more reading on streams with the sync it will do what you expect.
In vino veritas
|
|
|
|
|
|
Happy New Month to you all.
However, l am playing with Createfile, WriteFile and ReadFile functions, Createfile and Writefile works well but my problem is ReadFile.
l want to read the data written to a buffer "szBuffer", by using "ReadFile" to read the data in the szBuffer into another buffer szBuf. But it doesn't work.
When l check the return value like error=ReadFile(hfile,szBuffer,10,&in,NULL), it returns 0 but when l use GetLastError() to check for the return value, it gives me 998. l learnt that the second parameter to ReadFile() is the address to store the data read.
Code snippet below:
<pre
lang="c++">hfile=CreateFile("C:\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);// this works well
<pre>
WriteFile(hfile,szBuffer,256,&in,NULL);//this works well
ReadFile(hfile,szBuf,256,&in,NULL);//this does not read data
CloseHandle(hfile);
hdc=GetDC(hwnd);
TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError()));//check return value
ReleaseDC(hwnd,hdc);</pre>
Can someone help me to overcome this?
Thanks.
modified 4-Apr-16 9:59am.
|
|
|
|
|