DISM API functions.

Hello, everyone! This is my first post on the forums here, so feel free to provide any constructive criticism. So, I've been programming with c++ for a few weeks now and I've finally ran into my first problem I've been unable to resolve on my own. I'm looking to develop a program that uses the DISM api. I've attemped to compile a simple program from msdn to get comfortable with the DISM api and when I try and compile the program, I keep getting an "unresolved external symbol" error for every DISM function call ex (DismMountImage). I've installed the ADK and pointed visual studio to the include directory for DismApi.h, but still no luck. Mind you, I have about 3 weeks total of C++ programming experience, so I do apologize if this is a rather simple issue to fix. I will admit that the code sample is a bit above my current skill set, but any help would be greatly appreciated. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "windows.h"
#include <stdio.h>
#include "DismApi.h"
int _cdecl wmain()
{
	HRESULT hr = S_OK;
	HRESULT hrLocal = S_OK;
	DismSession session = DISM_SESSION_DEFAULT;
	DismSession sessionOnline = DISM_SESSION_DEFAULT;
	BOOL bMounted = FALSE;
	DismFeatureInfo *pFeatureInfo = NULL;
	DismString *pErrorString = NULL;
	// Initialize the API
	hr = DismInitialize(DismLogErrorsWarningsInfo, L"C:\\MyLogFile.txt", NULL);
	if (FAILED(hr))
	{
		wprintf(L"DismInitialize Failed: %x\n", hr);
		goto Cleanup;
	}
	wprintf(L"Mounting: ");
	// Mount a VHD image 
	hr = DismMountImage(L"C:\\Install.VHD",
		L"C:\\MountPath",
		1,
		NULL,
		DismImageIndex,
		DISM_MOUNT_READWRITE,
		NULL,
		NULL,
		NULL);
	if (FAILED(hr))
	{
		wprintf(L"\nDismMountImage Failed: %x\n", hr);
		goto Cleanup;
	}
	bMounted = TRUE;
	wprintf(L"Complete.\n\n");
	// Open a session against the mounted image
	hr = DismOpenSession(L"C:\\MountPath",
		NULL,
		NULL,
		&session);
	if (FAILED(hr))
	{
		wprintf(L"DismOpenSession Failed: %x\n", hr);
		goto Cleanup;
	}
	// Apply an unattend file to the offline image
	hr = DismApplyUnattend(session,
		L"C:\\unattend.xml",
		FALSE);
	if (FAILED(hr))
	{
		wprintf(L"DismApplyUnattend Failed: %x\n", hr);
		goto Cleanup;
	}
	// Also open a session against the running operating system (online image) 
	hr = DismOpenSession(DISM_ONLINE_IMAGE,
		NULL,
		NULL,
		&sessionOnline);
	if (FAILED(hr))
	{
		wprintf(L"DismOpenSession Failed: %x\n", hr);
		goto Cleanup;
	}
	// Get the feature info for a non-existent feature to demonstrate error
	// functionality
	hr = DismGetFeatureInfo(sessionOnline,
		L"ThisIsAFakeFeatureName",
		NULL,
		DismPackageNone,
		&pFeatureInfo);
	if (FAILED(hr))
	{
		wprintf(L"DismGetFeatureInfo Failed: %x\n", hr);
		hrLocal = DismGetLastErrorMessage(&pErrorString);
		if (FAILED(hrLocal))
		{
			wprintf(L"DismGetLastErrorMessage Failed: %x\n", hr);
			goto Cleanup;
		}
		else
		{
			wprintf(L"The last error string was: %s\n", pErrorString->Value);
		}
	}
Cleanup:
	// Delete the memory associated with the objects that were returned. 
	// pFeatureInfo should be NULL due to the expected failure above, but the
	// DismDelete function will still return success in this case. 
	hrLocal = DismDelete(pFeatureInfo);
	if (FAILED(hrLocal))
	{
		wprintf(L"DismDelete Failed: %x\n", hrLocal);
	}
	hrLocal = DismDelete(pErrorString);
	if (FAILED(hrLocal))
	{
		wprintf(L"DismDelete Failed: %x\n", hrLocal);
	}
	// Close the DismSession to free up resources tied to the offline session
	hrLocal = DismCloseSession(session);
	if (FAILED(hrLocal))
	{
		wprintf(L"DismCloseSession Failed: %x\n", hrLocal);
	}
	// Close the DismSession to free up resources tied to the online session
	hrLocal = DismCloseSession(sessionOnline);
	if (FAILED(hrLocal))
	{
		wprintf(L"DismCloseSession Failed: %x\n", hrLocal);
	}
	// Unmount the image if is has been mounted.  If the package was successfully added,
	// then commit the changes.  Otherwise, discard the
	// changes
	if (bMounted)
	{
		hrLocal = DismUnmountImage(L"C:\\MountPath",
			DISM_DISCARD_IMAGE,
			NULL,
			NULL,
			NULL);
		if (FAILED(hrLocal))
		{
			wprintf(L"DismUnmountImage Failed: %x\n", hrLocal);
		}
	}
	// Shutdown the DISM API to free up remaining resources
	hrLocal = DismShutdown();
	if (FAILED(hrLocal))
	{
		wprintf(L"DismShutdown Failed: %x\n", hr);
	}
	wprintf(L"Return code is: %x\n", hr);
	return hr;
}
You'll need to link against DismApi.lib I'm guessing.

#pragma comment(lib,"DismApi");
Last edited on
Thanks a lot Texan40! I'm away from my main PC right now so I cant test the change at this moment. When I can though, ill be sure to post the result.
Okay, So I configured Visual studio to link against the DismApi.lib as Texan40 described, but I'm still getting the "Unresovled External symbol" error every time I call one of the DISM API functions. Any other Ideas? Once again, any help would be greatly appreciated!
1. Turn on the verbose logging for the linking phase (the "Show Progress" setting on the Linker/General page of your project's properties.) and check that DismApi.lib is being processed as expected.

2. Dump the libraries exports and compare the function names in the lib against those the linker is expecting.

Open a Visual Studio Command Prompt and cd to the directory where you .lib is found the then use the following command to dump the lib's exports.

...\Deployment and Imaging Tools\SDKs\DismApi\Lib\>link /dump /linkermember:1 DismApi.lib

Andy

PS In your clean-up code you should probably protect more of the calls (like you are with if(bMounted)), e.g.

92
93
94
95
96
97
98
99
100
	if(pFeatureInfo != NULL)
	{
		hrLocal = DismDelete(pFeatureInfo);

		if (FAILED(hrLocal))
		{
			wprintf(L"DismDelete Failed: %x\n", hrLocal);
		}
	}


rather than

92
93
94
95
96
	hrLocal = DismDelete(pFeatureInfo);
	if (FAILED(hrLocal))
	{
		wprintf(L"DismDelete Failed: %x\n", hrLocal);
	}
Last edited on
Topic archived. No new replies allowed.