Solved

GetUserName WinDDK

Posted on 2011-02-18
1
859 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 32

Accepted Solution

by:
sarabande earned 250 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now