Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build and run/debug VeraCrypt on Visual Studio 2010 #1413

Open
AnhPC03 opened this issue Sep 7, 2024 · 10 comments
Open

Build and run/debug VeraCrypt on Visual Studio 2010 #1413

AnhPC03 opened this issue Sep 7, 2024 · 10 comments

Comments

@AnhPC03
Copy link

AnhPC03 commented Sep 7, 2024

Hi @idrassi, could I know how to build and run or debug the project on VS2010 or VS2019?
Because I want to change some code to print something when running the application that I can see in the console.

@idrassi
Copy link
Member

idrassi commented Sep 8, 2024

For now, debugging can be done by opening the VeraCrypt.sln solution in VS2010 or VS2019 and using the "All Debug" configuration to build the projects.

Regarding printing to the console, this is not possible by default because the VeraCrypt executable is a WIN32 GUI application that is not attached to a console. Therefore, printf and similar functions won't work.

However, you can programmatically allocate a console window for your WIN32 GUI application and redirect the standard I/O streams (stdout, stdin, and stderr) to the newly created console. This will allow you to use printf or std::cout to print logs.

Here is a function that creates a console and attaches it to the process:

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

void CreateConsole()
{
    // Allocate a console for this app
    if (!AllocConsole()) {
        MessageBox(NULL, "Failed to allocate console!", "Error", MB_OK | MB_ICONERROR);
        return;
    }

    FILE* fp;

    // Redirect stdout to console
    if (_wfreopen_s(&fp, L"CONOUT$", L"w", stdout) != 0) {
        MessageBox(NULL, "Failed to redirect stdout!", "Error", MB_OK | MB_ICONERROR);
        return;
    }

    // Redirect stdin to console
    if (_wfreopen_s(&fp, L"CONIN$", L"r", stdin) != 0) {
        MessageBox(NULL, "Failed to redirect stdin!", "Error", MB_OK | MB_ICONERROR);
        return;
    }

    // Redirect stderr to console
    if (_wfreopen_s(&fp, L"CONOUT$", L"w", stderr) != 0) {
        MessageBox(NULL, "Failed to redirect stderr!", "Error", MB_OK | MB_ICONERROR);
        return;
    }

    // Set console mode to support Unicode output
    SetConsoleOutputCP(CP_UTF8);
    SetConsoleCP(CP_UTF8);

    // Optionally, change the console mode to make it more usable for input
    HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    if (GetConsoleMode(hConsole, &mode)) {
        // Disable Quick Edit mode if needed
        SetConsoleMode(hConsole, mode & ~(ENABLE_QUICK_EDIT_MODE));
    }

    // Ensure std::wcout works correctly
    std::ios::sync_with_stdio(true);
    std::wcout.clear();
    std::wcin.clear();
    std::wcerr.clear();
}

You can then call CreateConsole in the InitApp function defined in Dlgcode.c:

void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
{
    WNDCLASSW wc;
    char langId[6];    
    SetDefaultDllDirectoriesPtr SetDefaultDllDirectoriesFn = NULL;
#if !defined(SETUP)
    wchar_t modPath[MAX_PATH];
#endif
    INITCOMMONCONTROLSEX InitCtrls;

    InitOSVersionInfo();

    CreateConsole(); // <-- Added

    // ... rest of the code
}

@AnhPC03
Copy link
Author

AnhPC03 commented Sep 9, 2024

@idrassi Thank you very much for your very detail answer. I'll try as your guide.

@AnhPC03
Copy link
Author

AnhPC03 commented Sep 9, 2024

Hi @idrassi, I did it again but got the error as in the picture. I chose All Debug and x64. I ran with Debug -> Start Without Debugging

I want to run the VeraCrypt application in VisualStudio and if I changed any code, if it has any errors when running, it shows in output of Visual Studio. Could you please help me?

Screen Shot 2024-09-09 at 22 35 59

@idrassi
Copy link
Member

idrassi commented Sep 9, 2024

In your screenshot, I see that the "Setup" project is in bold, which indicates that it is the default project when debugging. However, you cannot debug the Setup project this way because the final setup, which is distributed, must first be created from the setup generated using the /p switch.

To debug the setup installer, you need to build all binaries in debug mode (x86, x64, and ARM64), then run the batch file "Signing\sign_test_debug.bat". This batch file will sign the binaries using a test certificate and create a setup installer that can be debugged. You can read the batch file to get better understanding about the process.

The generated debug installer can then be debugged (Note: Visual Studio must be run as Administrator to debug the setup).

@AnhPC03
Copy link
Author

AnhPC03 commented Sep 10, 2024

@idrassi Thank. you for your explanation.
As I understand that after building solution and signing using "sign_test.bat" file, we will get the executable setup file. But this file only could be installed in Windows test mode, is it right?
And how if I want to build the setup file that could be installed on normal Windows?

@idrassi
Copy link
Member

idrassi commented Sep 10, 2024

No, you don't need to boot Windows into test mode to use the test binaries.

The only exception is if you rebuild the driver project, which is disabled by default. Officially signed drivers are included in the source code repository (under src\Release\Setup Files).

So, simply rebuild the solution in debug mode, run sign_test_debug.bat, and the generated setup installer can be used on a normal Windows system.

@AnhPC03
Copy link
Author

AnhPC03 commented Sep 11, 2024

@idrassi Thank you very much. I'll try it again

@AnhPC03
Copy link
Author

AnhPC03 commented Sep 12, 2024

@idrassi Hello, could you help me with this?
I saw the default Platform toolset is "Windows7.1SDK" but building solution failed because there is no "Windows7.1SDK" value in the Platform toolset list
Screen Shot 2024-09-12 at 10 51 44

@idrassi
Copy link
Member

idrassi commented Sep 12, 2024

You can change it v100. It is not mandatory to use Windows SDK 7.1.
You can refer to the Windows Build Guide in the documentation: https://veracrypt.fr/en/CompilingGuidelineWin.html

@AnhPC03
Copy link
Author

AnhPC03 commented Sep 12, 2024

@idrassi I installed and ran successfully all steps in Windows Build Guide including executing "sign_test.bat" and importing certificates but after that I sill couldn't install VeraCrypt by "VeraCrypt Setup 1.26.15.exe" file. The error when I clicked the setup file
Screen Shot 2024-09-12 at 14 18 48

There was one step that I hadn't tried yet
Compute SHA512 fingerprint of the test code signing certificate and update the gpbSha512CodeSignCertFingerprint array in the file "src/Common/Dlgcode.c" accordingly.

Could you explain how can I compute sha512 fingerprint of the test code signing certificate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants