VerQueryValue Access Violation

RLancaster
RLancaster used Ask the Experts™
on
Greetings,

To clarify my other question, and hopefulyy get some answers, here is my question again. The main problem is the fact that the code runs under Microsoft Visual C++ but when running in C++ Builder I get an Access Vioation on the line where VerQueryValue is called. Please can you help me.

I am using BORLAND C++ BUILDER 5
Runninf under WINDOWS XP PROFESSIONAL

Here is the code causing the problem:

    HMODULE hModule = GetModuleHandle(NULL);
    if (hModule)
    {
      HRSRC   hRes  = FindResource(hModule, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
      HGLOBAL hRegG = LoadResource(hModule, hRes);
      LPVOID  pVer  = LockResource(hRegG);
      if (pVer)
      {
        VS_FIXEDFILEINFO *ver = NULL;
        UINT nVerLen;
        if (VerQueryValue(pVer, TEXT("\\"), (LPVOID*)&ver, &nVerLen))
        {

          std::stringstream vstr;
          vstr << (ver->dwFileVersionMS >> 16)     << "."
               << (ver->dwFileVersionMS & 0xFFFF)  << "."
               << (ver->dwFileVersionLS >> 16)     << "."
               << (ver->dwFileVersionLS & 0xFFFF);

          ShowMessage(vstr.str().c_str());
        }
      }
    }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Why are you using FindResource() etc. to load the version information? As far as I know the recommended method of doing this is via the GetFileVersionInfoSize() and GetFileVersionInfo() functions.

Commented:
try this:

VS_FIXEDFILEINFO *ver = (VS_FIXEDFILEINFO*)malloc(sizeof(VS_FIXEDFILEINFO));
UINT nVerLen;
if (VerQueryValue(pVer, TEXT("\\"), (LPVOID*)&ver, &nVerLen))

You didn't initialize 'ver', so It cannot contain a value.  

Best Regards,
Tim
timussch,

quote:

lplpBuffer

Pointer to a variable that receives a pointer to the requested version information in the buffer pointed to by pBlock. The memory pointed to by *lplpBuffer is freed when the associated pBlock memory is freed

it receives a pointer, not a buffer, so it's ok
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

it would help if u check for nVerLen == 0, bcoz if it is, the structure contains undefined values
try using a IS_INTRESOURCE(VS_VERSION_INFO) to see whether it is a valid resource. though i suspect it is.

furthermore by locking the resource, u are not guaranteed that the start of the resource corresponds to the start of the structure
so i suggest u try  pjknibbs idea

Author

Commented:
Thanks for all the info ... problem solved.
I am now using the other method.

Commented:
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
You can always request to keep this question open. But remember, experts can only help if you provide feedback to their comments.
Unless there is objection or further activity,  I will suggest to accept

    "pjknibbs"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
========
Werner
Force accepted

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial