Solved

GetUserName WinDDK

Posted on 2011-02-18
1
867 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
1 Comment
 
LVL 33

Accepted Solution

by:
sarabande earned 250 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

809 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