|
Wait a minute - no matter what the screen resolution, the page doesn't fit on a 14" monitor and it does on a 17" monitor ? I think it's time for some details, like the resolutions you've used, some code, etc.
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
I am new to C++ and am self-teaching myself. I am trying to build a console app to perform several operations. One is the get the username. I keep getting an ERROR 122 (ERROR_INSUFFICIENT_BUFFER).
Can anyone look at my code and give assistance?
Here is my code:
================
char lpBuffer[256] = {0};
unsigned long nSize;
long lRetCode;
.
.
.
int GetUsrName()
{
lRetCode = GetUserName(lpBuffer,&nSize);
lRetCode = GetLastError();
if (lRetCode != ERROR_SUCCESS) {
LastError();
}
return (0);
}
|
|
|
|
|
According to the documentation, nSize recieves the size of the buffer required - have you checked this value ?
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
You must initialize nSize with the size of your buffer (256) before calling GetUserName().
|
|
|
|
|
Hy!
Actualy the 'nSize' parameters is an in/out parameter. This mean that when the function s called it should contain the size of the buffer in witch user name will be stored and when the function return it will contain the real number of characters contained in user name
Bye,
Orbital
|
|
|
|
|
I have made changes to the code and it now returns the Username however, I also now get a different error: (997) ERROR_IO_PENDING.
Here is the new code:
TCHAR lpBuffer[256];
unsigned long nSize = 256;
.
.
.
GetUserName(lpBuffer,&nSize);
printf("UserName:\t%s\n",lpBuffer);
|
|
|
|
|
Try this one:
char UserName[MAX_USERNAME_LEN];
DWORD len = MAX_USERNAME_LEN; GetUserName(UserName, &len);
Hope this helps
|
|
|
|
|
Hi all,
Do C++ got Panel like in Java ??
Let say if I want to create something like panel, how should I do it? Can I use group box to group everything together? I tried but it doesn't show like what panel can do. eg: can't hide every controls in that group.
Thanks.
FMansor
|
|
|
|
|
Normally if you create any window and create child windows (controls) related to it. Then you can just hide the main window and all is hidden!
Group box or normal static controls can do the job! However, if you are using the resource editor, then do the following:
in your WM_INITDIALOG try to make your group box the parent for the controls by calling SetParent. I am not sure if this will change their position, but give it a try!
|
|
|
|
|
I've tried to create a MainFrame app in MS VC6 by using the templates in VC6. I select creating an app using MFC with no documents, and what results is an app that only has a frame. Good! Yet when I compile the app and run it, the frame paints and dumps a deadly error from mfcocc with Abort Retry or Fail. The only option is to Abort. I have tried to look at the template for any problem, but I must admit, I'm only a novice. Any hints?
|
|
|
|
|
Hi!
How do you get access to the file name that you double click on, in the file explorer/ or on the desktop.
I have a custom application and a custom file extension. The application gets launched whenever I double
click on a file with that extension. However I am trying to obtain the name of the file that was double clicked.
AB
|
|
|
|
|
Did you check the command line?
Look at GetCommandLine() API!
Mh2!
|
|
|
|
|
Thanks!
Actually I was checking the command line, but when I created the registry key for my extension, I had not included the
%1 option! duh!
ab
|
|
|
|
|
Hi, I have a couple of questions regarding the usage of DLLs in a single process from within several threads. I am using dynamically loaded DLLs (to borrow a term from elsewhere on this website), that is I am calling AfxLoadLibrary and then GetProcAddress to find my functions. All of the dlls and the exe are built using MFC as a shared library. Please do not tell me I should be using COM to address any of these issues
1. Is there a way that the main thread in the EXE can call AfxLoadLibrary while a worker-thread is inside of a function in the DLL w/o causing lots of nasty problems? Right now I have hacked up a solution where it suspends any threads it thinks might be using the DLL, and it seems to work, but it causes memory leaks since that thread probably allocated memory inside of the DLL functions it might have called.
2. On a completely different note, would there be a way to call AfxLoadLibrary within each thread, and have each thread obtain its own HINSTANCE so that each thread could call AfxFreeLibrary independently of the others? I have tried to do this, and noticed that all copies of the dll functions seem to be the same. I know that in general, DLLs are loaded on a process basis rather than a thread basis, and was wondering if there was a way to override this behavior? If this were possible, #1 wouldn't be so much of an issue.
3. What is the safest way to share CWnd objects between threads, and within functions inside of DLLs? Basically, the main EXE opens the HINSTANCE to the dll, and then creates a separate thread to perform various lengthy operations in DLL functions, passing the thread an HWND (window created in EXE), and the thread then passes that HWND as an arguement into the GetProcAddress(ed) function. Inside the DLL, I call: CWnd::FromHandle. This seems to work if I use functions like SetWindowText or SendMessage, but does not work if I call PostMessage on that HWND (or use the CWnd wrapper). Since using SendMessage is dangerous because I don't want to overload the EXEs message pump, I'd really like to be able to use PostMessage. One weird issue here is that CStatic::SetWindowText works fine, while CProgressCtrl::SetPos does not seem to have any effect.
Mach5 Enterprises, LLC. http://www.mach5.com/
"Web Intelligence for the New Economy"
|
|
|
|
|
You can call AfxLoadLibrary from any thread to load a DLL. No problem with that!!
One thing with Dynamically loaded dlls is that you shouldn't use _declspec(thread) in your DLL!
If you want to use data per thread, then you should use tlsAlloc for thread local storage!
The only place that were you might hang is at startup... look at article Q142243 in MSDN
Regarding the memory leak, look at article Q105286. Note that the term USRDLL in the article is no longer valid, the article actually applies to all DLLs that use MFC
Mh2!
|
|
|
|
|
Ummm, its not exactly clear what the problem is. If you had stated what you are trying to acheive I'd have more to work with. However, I'll try. First off, you'll
be pleased to hear that COM has nothing to do with any of this.
1. Is there a way that the main thread in the EXE can call AfxLoadLibrary while a worker-thread is inside of a function in the DLL w/o causing lots of nasty problems? Right now I have hacked up a solution where it suspends any threads it thinks might be using the DLL, and it seems to work, but it causes memory leaks since that thread probably allocated memory inside of the DLL functions it might have called.
Once a DLL is loaded, subsequent calls to LoadLibrary just increment its reference count. So any worries about address space changes, or whatever it is you are worrying
about shouldn't matter.
2. On a completely different note, would there be a way to call AfxLoadLibrary within each thread, and have each thread obtain its own HINSTANCE so that each thread could call AfxFreeLibrary independently of the others? I have tried to do this, and noticed that all copies of the dll functions seem to be the same. I know that in general, DLLs are loaded on a process basis rather than a thread basis, and was wondering if there was a way to override this behavior? If this were possible, #1 wouldn't be so much of an issue.
Nope. The HINSTANCE returned is actually the load address (in the current Win32 implementation) of the DLL. YOu can check this in your debugger. Any calls to LoadLibrary after the first call, just increment the ref count and return the HINSTANCE as usual.
3. What is the safest way to share CWnd objects between threads, and within functions inside of DLLs? Basically, the main EXE opens the HINSTANCE to the dll, and then creates a separate thread to perform various lengthy operations in DLL functions, passing the thread an HWND (window created in EXE), and the thread then passes that HWND as an arguement into the GetProcAddress(ed) function. Inside the DLL, I call: CWnd::FromHandle. This seems to work if I use functions like SetWindowText or SendMessage, but does not work if I call PostMessage on that HWND (or use the CWnd wrapper). Since using SendMessage is dangerous because I don't want to overload the EXEs message pump, I'd really like to be able to use PostMessage. One weird issue here is that CStatic::SetWindowText works fine, while CProgressCtrl::SetPos does not seem to have any effect.
Using HWND is the way to go. Don't turn them in CWnd's as that requires access to the correct CWnd/HWND map which may not be in the thread you are calling from. USing
SendMessage is an absolute no-no in multithreaded code as you can get a deadlock.
You must use PostMessage or SendMessageTimeout. If you use PostMessage, your message
will not get processed until the thread that has the message loop processes the
message. This may be why you are not seeing the result of the posted message (the thread you are posting from may be too busy or in a higher priority group than the
message queue thread.
I don't know if those answers help, but maybe they'll help you post a clearer
explanation of the problem, or why you'd want multiple HINSTANCEs for the same DLL.
Stephen Kellett
--
C++/Java/Win NT/Unix variants
Memory leaks/corruptions/performance/system problems. UK based.
Problems with RSI/WRULD? Contact me for advice.
|
|
|
|
|
Thank you for your response.
To clarify what I'm doing:
1. EXE loads a library into a CWinApp derived member HINSTANCE, and also stores a bunch of
function pointers. This HINSTANCE is freed in the OnExitInstance of the EXE.
2. The EXE can launch a CWinThread derived class, passing a struct with function pointers. This happens an arbitrary number of times. The CWinThread thread uses a CDialog GUI.
3. Each GUI-based thread, in turn, responds to events in a dialog by creating worker threads and those worker threads call the DLL functions. The DLL functions are smart enough to take HANDLE arguements where necessary to distinguish between threads. All of this is working fine, and I don't think is the root of the problem. I can easily have the DLL functions accept only 1 thread at a time (i.e. they return false if they detect a 2nd thread) which I'm doing until I get the whole mess straightened out. Regardless, the problem is exactly the same with one thread as it is with many -- access violations when the EXE releases the HINSTANCE.
Everything works great until the main EXE wants to close. I set a flag in the DLL telling functions to return, and this works fine sometimes. The problem is, that if the main EXE closes, and there are lots of threads still "in" the DLL, and in particular, if they are "in" system functions inside the dll, they don't notice the halt-flag until it's too late, and they trigger an access violation because the EXE has already freed the DLL. I'm wondering if there's a way to have the AfxFreeLibrary start a cascase of reference counting instead of directly freeing the dll, so that the dll would stay alive until the last thread was halted, but I'm not dll-savvy enough to know how to do this. So this gets me back to my multiple-HINSTANCES thought, that if instead of having the AfxLoad/Release in the EXE it was per-thread, and if they were all independent of each other, then I wouldn't have any problems.
Thanks for your help!
Mach5 Enterprises, LLC. http://www.mach5.com/
"Web Intelligence for the New Economy"
|
|
|
|
|
Hey guys,
What I need is a standard library algorithm to find an element in an ordered vector... binary_search would be fine if it returned an iterator--?
Hope this isn't a dumb question, but all the suitable looking algorithms I found took linear time- surely what I need is in there, but where do I look?
Thanks,
nick
|
|
|
|
|
look at bsearch in < stdlib.h >
Mh2!
|
|
|
|
|
ForwardIterator lower_bound(ForwardIterator beg, ForwardIterator end, const T& value) returns the position of the first element greater than or equal to value. There's also a version that takes a binary comparision predicate. It's logarithmic time for random access iterators.
|
|
|
|
|
thanks a heap dude
|
|
|
|
|
I'm using SendInput in my application to simulate someone typing keys on the keyboard, but I'm thinking it's sending stuff to the target window's message queue too quickly for it to keep up. It's semi tolerable under Windows NT 4 Workstation, but when on a Windows 98 box almost twice as fast, almost half of the input doesn't make it through.
Anyone have any sort of experience with this thing? Or possible pointers/advice/etc? Thanks in advance
--
Peace,
Amit Jain
|
|
|
|
|
I am using SendInput and it is working fine on both Win9x and NT/Win2K
However, if the user is Interfering then he can definitely screw things!
Mh2!
|
|
|
|
|
Calling BlockInput before and after prevents the user from screwing things up most of the time...
--
Peace,
Amit Jain
|
|
|
|
|
My company is developing a custom keyboard and i would like some help on how to interface with the keyboard messages and stuff. Any help will be appreciated.
DrunkerII
|
|
|
|
|