Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

GetUserName WinDDK

Posted on 2011-02-18
1
Medium Priority
?
876 Views
Last Modified: 2013-12-14
There is an example in WinDDK called WatermakrUni. this example add watermark for pcl printer. watermark text is taken from user via user interface of printer proporties.

 i want to get username from windows so ican put it on page as watermark. When I try to compile source code with  GetUserName function,  compiler gives no error or warning.

But when I install plug in  it crash instantaly. program say acces vialotion.  when it comes to executeion of GetUserName functions. i am sending source code which I edit it

example in the this directory \WinDDK\7600.16385.1\src\print\oemdll\watermarkuni

error message =  0xC0000005: Access violation reading location 0x00000208.

GetUserName((LPWSTR)pOEMUIParam->pOEMOptItems[1].pSel,(LPDWORD)(MAX_PATH * sizeof(WCHAR))); // this is the part I added
0
Comment
Question by:Tungaalper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 35

Accepted Solution

by:
sarabande earned 1000 total points
ID: 34941182
access violation is cause the second argument of GetUserName is pointer to a long word that should take the length of the name. you made a cast of a constant to LPDWORD what crashes when the function assigns a value to the address the pointer is pointing to.

the statement also crashes if one of pOEMUIParam or pOEMUIParam->pOEMOptItems or pOEMUIParam->pOEMOptItems[1] or pOEMUIParam->pOEMOptItems[1].pSel  is invalid pointer or NULL.

i don't know why you use a so complicated variable for first argument of GetUserName which is simply a char or wchar_t buffer

so use

   TCHAR buffer[1024];
   DWORD len = sizeof(buffer) / sizeof(buffer[0]);
   BOOL ret = GetUserName(buffer, &len);

and it will work.

Sara
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question