ImageX and using window imaging from C++

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="margin:0cm 0cm 0pt <span style="font-size:small <span style="font-family:Calibri Hi Forum,
<p style="margin:0cm 0cm 0pt <span style="font-size:small; font-family:Calibri
<p style="margin:0cm 0cm 0pt <span style="font-size:small <span style="font-family:Calibri I am attempting to using Windows Imaging API from my own program to restore an image.<span style="

<p style="margin:0cm 0cm 0pt <span style="font-size:small; font-family:Calibri
<p style="margin:0cm 0cm 0pt <span style="font-size:small <span style="font-family:Calibri To restore an example image ImageX can do this in 3min 25sec.<span style="
However from my code I get a total restore time of 9min 50sec.
<p style="margin:0cm 0cm 0pt <span style="font-size:small; font-family:Calibri
<p style="margin:0cm 0cm 0pt <span style="font-size:small <span style="font-family:Calibri I have captured some time information from the WIM calls and the attached program spend 3min 18sec in the WIMCreateFile and 6 min 41 sec in WIMApplyImage.
<p style="margin:0cm 0cm 0pt <span style="font-size:small; font-family:Calibri
<p style="margin:0cm 0cm 0pt <span style="font-size:small <span style="font-family:Calibri Does anyone know how imageX does the restore to achieve the times it gets?
<p style="margin:0cm 0cm 0pt <span style="font-size:small; font-family:Calibri
<p style="margin:0cm 0cm 0pt <span style="font-size:small <span style="font-family:Calibri Thanks
<p style="margin:0cm 0cm 0pt <span style="font-size:small; font-family:Calibri
<p style="margin:0cm 0cm 0pt <span style="font-size:small; font-family:Calibri D
<p style="margin:0cm 0cm 0pt
<div style="color:Black;background-color:White; <pre>
#include <stdio.h>
#include <windows.h>
#include <wimgapi.h>
#define IN
#define INOUT
<span style="color:Green; //
<span style="color:Green; //Callback function:
<span style="color:Green; //
DWORD WINAPI ApplyCallback(DWORD dwMessageId, WPARAM wParam, LPARAM lParam, PVOID pvUserData) {

<span style="color:Blue; switch ( dwMessageId ) {

<span style="color:Blue; case WIM_MSG_SETRANGE: <span style="color:Green; // http://msdn.microsoft.com/en-us/library/7535a5c5-f72f-4c1e-90e9-d07a8d4132cd
printf (<span style="color:#A31515; "File count to be restored %dn", (DWORD) lParam);
<span style="color:Blue; break;

<span style="color:Blue; case WIM_MSG_SETPOS: <span style="color:Green; // http://msdn.microsoft.com/en-us/library/dd834954%28v=MSDN.10%29.aspx
<span style="color:Blue; if ((DWORD) lParam == 0) {
printf (<span style="color:#A31515; "Starting applyn");
} <span style="color:Blue; else {
printf (<span style="color:#A31515; "Finished applyn");
}
<span style="color:Blue; break;

<span style="color:Blue; case WIM_MSG_PROGRESS: <span style="color:Green; // http://msdn.microsoft.com/en-us/library/f0c291f5-2a40-4805-8d00-82e8b852275c
printf (<span style="color:#A31515; "rPercentage %d", (UINT) wParam);
<span style="color:Blue; if ((UINT) wParam == 100) {
printf (<span style="color:#A31515; "n");
}
<span style="color:Blue; break;
}
<span style="color:Blue; return WIM_MSG_SUCCESS;
}

LPWSTR MultiCharToUniChar(<span style="color:Blue; char* multiByteString) {
size_t len = strlen(multiByteString) + 1;
<span style="color:Blue; wchar_t *wideCharString = <span style="color:Blue; new <span style="color:Blue; wchar_t[len];
mbstowcs(wideCharString, multiByteString, len); <span style="color:Green; // Convert multibyte string to wide-character string
<span style="color:Blue; return (LPWSTR)wideCharString;
}

<span style="color:Blue; int main(DWORD argc, <span style="color:Blue; char *argv[])
{
HANDLE WIMHandle = NULL;
HANDLE ImageHandle = NULL;
<span style="color:Blue; char wimSourceFile[] = <span style="color:#A31515; "KDClient.WIM";
<span style="color:Blue; char wimApplyDirectory[] = "C:\";

<span style="color:Blue; bool success = <span style="color:Blue; false;

printf (<span style="color:#A31515; "Restoring up %s to %sn", wimSourceFile, wimApplyDirectory);

<span style="color:Blue; char tempDirectory [MAX_PATH] = {0};
GetTempPath(MAX_PATH, tempDirectory);

printf (<span style="color:#A31515; "Temp path being used is %sn", tempDirectory);
LPWSTR WIMTempDirectory = MultiCharToUniChar (tempDirectory);
LPWSTR WIMSourceFile = MultiCharToUniChar (wimSourceFile);
LPWSTR WIMApplyDirectory = MultiCharToUniChar (wimApplyDirectory);

<span style="color:Blue; if (WIMRegisterMessageCallback( NULL, (FARPROC) ApplyCallback, NULL ) != INVALID_CALLBACK_VALUE) {
DWORD created = 0;
WIMHandle = WIMCreateFile (WIMSourceFile, WIM_GENERIC_READ, WIM_OPEN_EXISTING, WIM_FLAG_VERIFY, 0, &created );
<span style="color:Blue; if (WIMHandle) {
<span style="color:Blue; if (WIMSetTemporaryPath(WIMHandle, WIMTempDirectory)) {
DWORD imgIndex = 1;
ImageHandle = WIMLoadImage (WIMHandle, imgIndex);
<span style="color:Blue; if (ImageHandle) {
<span style="color:Blue; if (WIMApplyImage (ImageHandle, WIMApplyDirectory, WIM_FLAG_VERIFY)) {
success = <span style="color:Blue; true;
} <span style="color:Blue; else {
printf (<span style="color:#A31515; "Failed to apply image. Error code %dn", GetLastError());
}
<span style="color:Blue; if (!WIMCloseHandle(ImageHandle)) {
printf (<span style="color:#A31515; "Failed to close image device correctly. Error code %dn", GetLastError());
<span style="color:Blue; if (success) success = <span style="color:Blue; false;
}
} <span style="color:Blue; else {
printf (<span style="color:#A31515; "Failed to load image. Error code %dn", GetLastError());
}
} <span style="color:Blue; else {
printf (<span style="color:#A31515; "Unable to set temp direcotryn");
}
<span style="color:Blue; if (!WIMCloseHandle(WIMHandle)) {
printf (<span style="color:#A31515; "Failed to WIM file correctly. Error code %dn", GetLastError());
<span style="color:Blue; if (success) success = <span style="color:Blue; false;
}
} <span style="color:Blue; else {
printf (<span style="color:#A31515; "Cannot open the WIM file. Error code %dn", GetLastError());
}
} <span style="color:Blue; else {
printf (<span style="color:#A31515; "Unable to register capture callback.n");
}
<span style="color:Blue; return 0;
}
[/code]
<p style="margin:0cm 0cm 0pt <br/>



View the full article
 
Back
Top