Xpert Coder

New Member
Joined
Sep 24, 2012
Messages
2
Dear All,


I need some help regarding a strange issue with CryptUnprotectData function.


I have a C++ code that reads the database password from the registry and decrypts it using CryptUnprotectData. We have to deploy this application on a Windows 7 machine.


When I compile my code on Windows XP and run it on the test Windows 7 machine, it works absolutely fine. When I compile the same code on my laptop having Windows 7 and run it on the test Windows 7 machine, CryptUnprotectData fails with GetLastError() return '87'. If I run this application on my own laptop with Windows 7 on it, it again works fine probably because my laptop has a lot of things installed including Visual Studio and all the service packs etc.


I believe I have missed out installing some dependency on the test Windows 7 machine but I am unable to figure out what is that. What is it that's making the Windows XP compiled code running fine on the test machine and not the code that's compiled on Windows 7.


Here is the call to CryptUnprotectData in the code:


if (CryptUnprotectData(
&cipherText,
NULL,
NULL, // Optional entropy
NULL, // Reserved
NULL, // Optional PromptStruct
CRYPTPROTECT_UI_FORBIDDEN,
&plainText))
{
password.Append((char*)plainText.pbData, plainText.cbData);
}
else
{
char buff[256];
sprintf (buff, "CryptUnprotectData Error %d", GetLastError());
AfxMessageBox(buff);
}




Please suggest. Thanks in advance.
 


Solution
The issue you are facing with CryptUnprotectData returning error code '87' (ERROR_INVALID_PARAMETER) on the Windows 7 test machine when running the code compiled on a Windows 7 laptop could be due to environmental differences in the two systems, leading to different behaviors. Here are a few things you can consider to troubleshoot and resolve this problem:
  1. Check CryptUnprotectData Parameters:
    • Ensure that all parameters passed to the CryptUnprotectData function are correct.
    • Check if the &cipherText parameter is valid and properly initialized.
    • Verify that the &plainText parameter is correctly initialized and allocated before calling CryptUnprotectData.
    []Compatibility Settings...
The issue you are facing with CryptUnprotectData returning error code '87' (ERROR_INVALID_PARAMETER) on the Windows 7 test machine when running the code compiled on a Windows 7 laptop could be due to environmental differences in the two systems, leading to different behaviors. Here are a few things you can consider to troubleshoot and resolve this problem:
  1. Check CryptUnprotectData Parameters:
    • Ensure that all parameters passed to the CryptUnprotectData function are correct.
    • Check if the &cipherText parameter is valid and properly initialized.
    • Verify that the &plainText parameter is correctly initialized and allocated before calling CryptUnprotectData.
    []Compatibility Settings:
    • Make sure that the application compiled on Windows 7 is set to run in compatibility mode for Windows 7 on the test machine. This ensures that the application behaves as expected on different Windows versions.
    [
    ]Dependency Differences:
    • Compare the system dependencies and runtime libraries on both Windows 7 machines. Make sure that all necessary dependencies are present on the test Windows 7 machine.
    • Check for any missing redistributable packages required by your application. Visual C++ Redistributable Packages might be needed for C++ applications.
    []Compiler and SDK Versions:
    • Verify that the compiler versions and SDK used to compile the code on Windows XP and Windows 7 are compatible with the target Windows 7 machine.
    • Check if there are any differences in the compilation flags or settings that might affect the behavior of CryptUnprotectData.
    [
    ]Debugging:
    • Add more error checking and log information to pinpoint where exactly the error is occurring.
    • Use debugging tools like Visual Studio Debugger to trace the execution flow and identify the exact point of failure.
    []Update Windows 7:
    • Ensure that the test Windows 7 machine is up to date with all Windows updates, including security patches and service packs.
    [
    ]Error Handling:
    • Improve error handling in your code to provide more detailed information about the failure. Logging the input parameters and other relevant details can help in debugging. By investigating these aspects and comparing the runtime environments of the two Windows 7 machines, you should be able to track down the root cause of the CryptUnprotectData error '87' on the test machine and address it accordingly.
 


Solution
Back
Top