|
Thanks a lot for your effort.., i ll try this one out and tell you if it works
|
|
|
|
|
YEH!!!!!!!! it worked. Thanks once again.
But what is the use of "_T". if i compile without including it, the application still works fine.
|
|
|
|
|
The _T is a macro provided by TCHAR.H for unicode/multilingual support. If you go to the project settings->general tab->Character set you will have that to "not set" setting. That macro allows you to use the other choices being unicode and multi-byte character sets making your code work multilingual like in chineese windows.
For you in ascii mode the macro actually does nothing (which you worked out) but if you select the other modes you will see you will get an error on every static text the _T tells the compiler to make the string in the correct mode and removes the error.
Being a commercial programmer and as Microsoft has made it so easy for doing it we generally try and use the multilingual code calls since Visual Studio 2013. This became almost compulsory when trying to write true 64 bit applications. The default setting of an empty project is actually to set for unicode character set.
Essentially TCHAR becomes a replacement for the standard char and its size varies in the compilation modes. They provide new string functions that match the old string functions in TCHAR.H but have different code for the different modes. Lets give you an example
strlen becomes _tcslen those calls work identical the difference being _tcslen will work in any language mode compilation, while strlen will only work in language "not set" mode like you have. Here is the link to what is going on from MSDN
strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l[^]
This code is designed specifically for windows (it uses the Win32 API), it is not general in nature like that could work on linux so there is no reason to write generically but we should try and cover the different modes of windows compilation, especially as it is easy.
So for me the changes are just habit.
There is a funny part of this that so many of us are writing in that style that the linux community is having issues trying to port our code. So if writing general code I would probably try to avoid this style of programming.
In vino veritas
modified 8-Apr-16 23:28pm.
|
|
|
|
|
Hi
I am using Mailslots for Interprocess communication. Process A does a WriteFile with NULL for the overlapped parameter.
Process B has a overlapped parameter on the ReadFile.
My question is Can process A do WaitForSingleObject on the hEvent of process B overlapped to know when the read has completed
Thsnks
|
|
|
|
|
|
I did in a ReadFile for a mail slot when the i/o is completed shouldn't hOverLap.hEvent be signaled ?
|
|
|
|
|
|
|
I read the article
Which basically Said if I CreateFile on the Client Side with FILE_FLAG_OVERALAPPED
File then when doing an I/O which I would assume would include ReadFile on the Server side
An Overlapped structure would be used (A member of which is m_hEvent
I did a OpenEvent on the Client side as The CreateProcess let me inherit objects
tried WaitForSingleObject (m_hEvent of the overlapped of the ReadFile)
the First wait worked the second didn't as I did three writes
|
|
|
|
|
ForNow wrote: First wait worked the second didn't as I did three writes
"Didn't work" is hardly a technical description of the problem. Are you forgetting to reset a manual-reset event?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Don't think so the flag is set to False it should auto-reset
sysblk.mail = CreateEvent(&sa,
FALSE, FALSE, (LPCTSTR)"MyEvent");
|
|
|
|
|
Well maybe the writes are occurring so quickly that the system only has time to signal the event one time.
Whenever the event is signaled, you should check for more messages before looping back to the wait function.
Don't expect the event to be signaled exactly once for each message you write.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You are probably right the WriteFile has a NULL for the overlapped While the ReadFile has a overlapped paramater
|
|
|
|
|
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;
}
|
|
|
|
|