Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7638097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:05:28+00:00 2026-05-31T08:05:28+00:00

Previously, I’d just dealt with these types of __out function parameters using malloc, but

  • 0

Previously, I’d just dealt with these types of __out function parameters using malloc, but I’m trying to change my ways.

As a specific example, in a class to manage Raw Input, GetRawInputDeviceInfo() is prototyped as such:

UINT GetRawInputDeviceInfo(HANDLE, UINT, LPVOID, PUINT)

LPVOID is a pointer to a buffer containing the information I need. PUINT is a pointer to a UINT containing the size of data contained in the buffer pointed to by LPVOID.

Normally, I would (once I have populated the PUINT):

PUINT cbSize; // assume it is sized correctly and contains the proper
              // length of data

LPVOID buffer = (LPVOID)malloc(sizeof(&cbSize));
GetRawInputDeviceInfo(XXX.handle, RIDI_DEVICENAME, buffer, cbSize);
//do something w/buffer
free(buffer);

Now, attempting to do this without malloc, I would write:
(sorry, I’m typing this from work, so I may botch this from memory)

PUINT cbsize; // assume it is sized correctly and contains the proper
              // length of data

1 of the following declaration and use examples:
LPVOID unique_ptr:

std::unique_ptr<LPVOID> buffer;
GetRawInputDeviceInfo(xxx.handle, RIDI_DEVICENAME, buffer.get(),
                      cbSize);

UINT unique_ptr:

std::unique_ptr<UINT> buffer;
GetRawInputDeviceInfo(xxx.handle, RIDI_DEVICENAME,
                      (LPVOID)buffer.get(), cbSize);

Raw UINT Pointer:

UINT *buffer = NULL;
GetRawInputDeviceInfo(xxx.handle, RIDI_DEVICENAME,
                      (LPVOID)buffer, cbSize);

Then reading the buffer:

OutputDebugString((LPCSTR)buffer) //add .get() for unique_ptr

The thing is, the buffer contains the information I want, and it’s outputted as it should be! However, when the unique_ptr goes out of scope and is deleted (or the UINT* is deleted), I get a Heap Corruption exception. I stepped through the code and what happens is once the GetRawInputDeviceInfo function runs, ALL of my class level containers/variables have their data rewritten. For example, the above sequence appears in a for loop, and my iterator goes from 0 (first iteration) to 80837436 (or so), and all other variables local variables are messed up.

So, how can I retrieve the information in the buffer without screwing everything else up? And preferably without using malloc/free, and with the spirit of RAII 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:05:30+00:00Added an answer on May 31, 2026 at 8:05 am

    The correct way to use GetRawInputDeviceInfo is

    1. Get the number of characters the name contains

      UINT char_count;
      GetRawInputDeviceInfo(xxx.handle, RIDI_DEVICENAME, NULL, &char_count);
      
    2. Allocate a long enough string buffer, and retrieve the name

      std::unique_ptr<wchar_t[]> buf (new wchar_t[char_count]);
      GetRawInputDeviceInfo(xxx.handle, RIDI_DEVICENAME, buf.get(), &char_count);
      

    Your example code won’t cause heap corruption. Probably your real code uses an uninitialized buffer, which caused GetRawInputDeviceInfo to write data to some unintended location.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Previously I have only really worked on applications, but now I am trying to
Previously, this function worked for me... $this_day = Day_of_Week($lyear, $month, $day); using this lib..
Previously I was using CBR_9600 when communicating with 9600 baud devices. But there does
Previously we had desktop applications but given the fact that accessing the server (either
Previously i was able to download YouTube videos as mp3 via youtube-mp3.org Using this
Previously I had: MyClass::MyClass() : myMember(VALUE,this,5,etc.) { ... } but I need to do
Previously, WinAPI::shellExecute() is working on AX 2009, but I do not know why, after
Previously i have used restful wcf webservices to get the data from server.But now
Previously, I could build projects and launch the sites..but suddenly, it stopped building and
Previously I've worked alone... Now I may have many collaborators. But I don't want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.