VS 2013选Release模式为何得到的exe在别的电脑上还是运行不了?

哈利_蜘蛛侠 2014-04-07 08:34:22
就是弹出一个错误窗口,提示说
缺少MSVCR120.dll
这个是什么情况?

如果这个问题不够具体的话,我只好把代码放出来了,很长,分三个部分。要用到DX 9相关的头文件和.lib文件:

d3dUtility.h

#ifndef __d3dUtilityH__
#define __d3dUtilityH__

#include <windows.h>
#include <d3dx9.h>
#include <string>

namespace d3d
{
// global constants
const char g_szClass[] = "Direct3D9App";
const char g_szCaption[] = "Direct3D9App";

const int WNDWIDTH = 640;
const int WNDHEIGHT = 480;

bool InitD3D(
HINSTANCE hInstance, // [in] Application instance.
int width, int height, // [in] Backbuffer dimensions.
bool windowed, // [in] Windowed (true) or full screen (false).
D3DDEVTYPE deviceType, // [in] HAL or REF
IDirect3DDevice9** device); // [out] The created device.

int EnterMsgLoop(
bool(*ptr_display)(float timeDelta));

LRESULT CALLBACK WndProc(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam);

template<class T> void Release(T t)
{
if (t)
{
t->Release();
t = 0;
}
}

template<class T> void Delete(T t)
{
if (t)
{
delete t;
t = 0;
}
}
const D3DXCOLOR WHITE(D3DCOLOR_XRGB(255, 255, 255));
const D3DXCOLOR BLACK(D3DCOLOR_XRGB(0, 0, 0));
const D3DXCOLOR RED(D3DCOLOR_XRGB(255, 0, 0));
const D3DXCOLOR GREEN(D3DCOLOR_XRGB(0, 255, 0));
const D3DXCOLOR BLUE(D3DCOLOR_XRGB(0, 0, 255));
const D3DXCOLOR YELLOW(D3DCOLOR_XRGB(255, 255, 0));
const D3DXCOLOR CYAN(D3DCOLOR_XRGB(0, 255, 255));
const D3DXCOLOR MAGENTA(D3DCOLOR_XRGB(255, 0, 255));
//
// Lights
//
D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color);
D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color);
D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color);
//
// Materials
//
D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p);

const D3DMATERIAL9 WHITE_MTRL = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);
const D3DMATERIAL9 RED_MTRL = InitMtrl(RED, RED, RED, BLACK, 2.0f);
const D3DMATERIAL9 GREEN_MTRL = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);
const D3DMATERIAL9 BLUE_MTRL = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);
} // end of namespace d3d

#endif // __d3dUtilityH__



d3dUtility.cpp

#include "d3dUtility.h"

bool d3d::InitD3D(
HINSTANCE hInstance,
int width, int height,
bool windowed,
D3DDEVTYPE deviceType,
IDirect3DDevice9** device)
{
//
// Create the main application window.
//
WNDCLASSEX wc;

wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)d3d::WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClass;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wc))
{
::MessageBox(NULL, "RegisterClassEx() - FAILED", "Error", MB_OK);
return false;
}

HWND hwnd = 0;
hwnd = ::CreateWindow(g_szClass, g_szCaption,
WS_EX_TOPMOST,
0, 0, width, height,
NULL /*parent hwnd; 原版是 0 */, NULL/*menu; 原版是 0 */,
hInstance, NULL/*extra;原版是 0 */);
if (!hwnd)
{
::MessageBox(NULL, "CreateWindow() - FAILED", "Error", MB_OK);
return false;
}
::ShowWindow(hwnd, SW_SHOW);
::UpdateWindow(hwnd);
//
// Init D3D:
//
HRESULT hr = 0;
// Step 1: Create the IDirect3D9 object.
IDirect3D9* d3d9 = 0;
d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if (!d3d9)
{
::MessageBox(NULL, "Direct3DCreate9() - FAILED", "Error", MB_OK);
return false;
}
// Step 2: Check for hardware vp.
D3DCAPS9 caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);

int vp = 0;
if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

// Step 3: Fill out the D3DPRESENT_PARAMETERS structure.
D3DPRESENT_PARAMETERS d3dpp;
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Windowed = windowed;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

// Step 4: Create the device.
hr = d3d9->CreateDevice(
D3DADAPTER_DEFAULT, // primary adapter
deviceType, // device type
hwnd, // window associated with device
vp, // vertex processing
&d3dpp, // present parameters
device); // return created device

if (FAILED(hr))
{
// try again using a 16-bit depth buffer
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

hr = d3d9->CreateDevice(
D3DADAPTER_DEFAULT,
deviceType,
hwnd,
vp,
&d3dpp,
device);

if (FAILED(hr))
{
d3d9->Release(); // done with d3d9 object
// 原版是 ::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
::MessageBox(NULL, "CreateDevice() - FAILED", "Error", MB_OK);
return false;
}
}
d3d9->Release(); // done with d3d9 object
return true;
}

int d3d::EnterMsgLoop(bool (*ptr_display)(float timeDelta))
{
MSG msg;
::ZeroMemory(&msg, sizeof(MSG));

static float lastTime = (float)timeGetTime();

while (msg.message != WM_QUIT)
{
if (::PeekMessage(&msg, NULL/*原版是 0 */, 0, 0, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else
{
float currTime = (float)timeGetTime();
float timeDelta = (currTime - lastTime)*0.001f;

ptr_display(timeDelta);

lastTime = currTime;
}
}
return msg.wParam;
}

D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)
{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));

light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = *color * 0.6f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Direction = *direction;

return light;
}

D3DLIGHT9 d3d::InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));

light.Type = D3DLIGHT_POINT;
light.Ambient = *color * 0.6f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Position = *position;
light.Range = 1000.0f;
light.Falloff = 1.0f;
light.Attenuation0 = 1.0f;
light.Attenuation1 = 0.0f;
light.Attenuation2 = 0.0f;

return light;
}

D3DLIGHT9 d3d::InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));

light.Type = D3DLIGHT_SPOT;
light.Ambient = *color * 0.0f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Position = *position;
light.Direction = *direction;
light.Range = 1000.0f;
light.Falloff = 1.0f;
light.Attenuation0 = 1.0f;
light.Attenuation1 = 0.0f;
light.Attenuation2 = 0.0f;
light.Theta = 0.4f;
light.Phi = 0.9f;
return light;
}

D3DMATERIAL9 d3d::InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
{
D3DMATERIAL9 mtrl;
mtrl.Ambient = a;
mtrl.Diffuse = d;
mtrl.Specular = s;
mtrl.Emissive = e;
mtrl.Power = p;
return mtrl;
}


...全文
293 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-04-09
  • 打赏
  • 举报
回复
引用 13 楼 u011873969 的回复:
[quote=引用 10 楼 xiaohuh421 的回复:] 使用depends工具查看即可 那个是什么工具?在哪里?
C:\Microsoft SDK\Bin\Depends.Exe C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin\Depends.Exe C:\Program Files\Microsoft Visual Studio\Common\Tools\DEPENDS.EXE C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin\Depends.Exe C:\SupportTools\depends.exe
hust_wangyajun 2014-04-08
  • 打赏
  • 举报
回复
我会告诉你depends中问号标识的就是缺少的dll 也可以直接运行看缺什么dll 找到后放在exe所在目录
赵4老师 2014-04-08
  • 打赏
  • 举报
回复
用depends.exe查看exe依赖的dll
xiaohuh421 2014-04-08
  • 打赏
  • 举报
回复
使用depends工具查看即可
引用 9 楼 u011873969 的回复:
如何知道需要哪些动态链接库呢?
哈利_蜘蛛侠 2014-04-08
  • 打赏
  • 举报
回复
[quote=引用 10 楼 xiaohuh421 的回复:] 使用depends工具查看即可 那个是什么工具?在哪里?
哈利_蜘蛛侠 2014-04-07
  • 打赏
  • 举报
回复
引用 7 楼 max_min_ 的回复:
缺少动态库dll啊!动态库是在程序运行的时候才加载到程序里的! 所以打包的时候,需要和exe一起打包的
如何知道需要哪些动态链接库呢?
哈利_蜘蛛侠 2014-04-07
  • 打赏
  • 举报
回复
引用 6 楼 yiyefangzhou24 的回复:
一大串代码,其实就是个连接设置问题 http://blog.csdn.net/educast/article/details/7837612
好的,多谢了,哈哈哈哈哈哈!
max_min_ 2014-04-07
  • 打赏
  • 举报
回复
缺少动态库dll啊!动态库是在程序运行的时候才加载到程序里的! 所以打包的时候,需要和exe一起打包的
yiyefangzhou24 2014-04-07
  • 打赏
  • 举报
回复
一大串代码,其实就是个连接设置问题 http://blog.csdn.net/educast/article/details/7837612
哈利_蜘蛛侠 2014-04-07
  • 打赏
  • 举报
回复
引用 2 楼 FreeFice 的回复:
交付的版本应该去掉动态链接库选项,除非你将动态库一起移交发布。
或者,如何知道用到了那些动态链接库?知道了哪些动态链接库后,是不是直接找到,添加进去发布就可以了?
哈利_蜘蛛侠 2014-04-07
  • 打赏
  • 举报
回复
引用 2 楼 FreeFice 的回复:
交付的版本应该去掉动态链接库选项,除非你将动态库一起移交发布。
请问如何去掉呢?
buyong 2014-04-07
  • 打赏
  • 举报
回复
写一个hello world,看是不是也有这种情况。
庄鱼 2014-04-07
  • 打赏
  • 举报
回复
交付的版本应该去掉动态链接库选项,除非你将动态库一起移交发布。
哈利_蜘蛛侠 2014-04-07
  • 打赏
  • 举报
回复
spotLight.cpp 这个是WinMain函数所在的地方:

#include "d3dUtility.h"

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "winmm.lib")

//
// Globals
//

IDirect3DDevice9* Device = 0;

ID3DXMesh* Objects[4] = { 0, 0, 0, 0 };
D3DXMATRIX Worlds[4];
D3DMATERIAL9 Mtrls[4];

D3DLIGHT9 Spot;

//
// Framework Functions
//

bool Setup()
{
	// Create objects.
	D3DXCreateTeapot(Device, &Objects[0], 0);
	D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0);
	D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0);
	D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0);
	//
	// Build world matrices - position the objects in world space.
	//
	D3DXMatrixTranslation(&Worlds[0], 0.0f, 2.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[1], 0.0f, -2.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[2], -3.0f, 0.0f, 0.0f);
	D3DXMatrixTranslation(&Worlds[3], 3.0f, 0.0f, 0.0f);
	D3DXMATRIX Rx;
	D3DXMatrixRotationX(&Rx, D3DX_PI*0.5f);
	Worlds[3] = Rx*Worlds[3];
	//
	// Setup the objects' materials.
	//
	Mtrls[0] = d3d::RED_MTRL;
	Mtrls[1] = d3d::BLUE_MTRL;
	Mtrls[2] = d3d::GREEN_MTRL;
	Mtrls[3] = d3d::YELLOW_MTRL;

	for (int i = 0; i < 4; ++i)
		Mtrls[i].Power = 20.f;
	//
	// Setup a spot light
	//
	D3DXVECTOR3 pos(0.0f, 0.0f, -5.0f);
	D3DXVECTOR3 dir(0.0f, 0.0f, 1.0f);
	D3DXCOLOR c = d3d::WHITE;
	Spot = d3d::InitSpotLight(&pos, &dir, &c);
	//
	// Set and Enable spotlight.
	//
	Device->SetLight(0, &Spot);
	Device->LightEnable(0, true);
	//
	// Set light related render states.
	//
	Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
	Device->SetRenderState(D3DRS_SPECULARENABLE, true);
	//
	// Position and aim the camera.
	//
	D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &position, &target, &up);
	Device->SetTransform(D3DTS_VIEW, &V);
	//
	// Set the projection matrix.
	//
	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
		&proj,
		D3DX_PI * 0.5f, // 90 - degree
		(float)d3d::WNDWIDTH / (float)d3d::WNDHEIGHT,
		1.0f,
		1000.0f);
	Device->SetTransform(D3DTS_PROJECTION, &proj);
	return true;
}

void Cleanup()
{
	for (int i = 0; i < 4; ++i)
		d3d::Release<ID3DXMesh*>(Objects[i]);
}

bool Display(float timeDelta)
{
	if (Device)	// Only use Device methods if we have a valid device.
	{
		//
		// Move spot light around based on keyboard input
		//
		static float angle = (3.0f*D3DX_PI) / 2.0f;
		if (::GetAsyncKeyState(VK_LEFT) & 0x8000f)
			Spot.Direction.x -= 0.5f*timeDelta;
		if (::GetAsyncKeyState(VK_RIGHT)& 0x8000f)
			Spot.Direction.x += 0.5f * timeDelta;
		if (::GetAsyncKeyState(VK_DOWN) & 0x8000f)
			Spot.Direction.y -= 0.5f * timeDelta;
		if (::GetAsyncKeyState(VK_UP) & 0x8000f)
			Spot.Direction.y += 0.5f * timeDelta;
		// update the light
		Device->SetLight(0, &Spot);
		Device->LightEnable(0, true);
		//
		// Draw the scene:
		//
		Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
		Device->BeginScene();
		for (int i = 0; i < 4; ++i)
		{
			// set material and world matrix for ith object, then render
			// the ith object.
			Device->SetMaterial(&Mtrls[i]);
			Device->SetTransform(D3DTS_WORLD, &Worlds[i]);
			Objects[i]->DrawSubset(0);
		}

		Device->EndScene();
		Device->Present(0, 0, 0, 0);
	}

	return true;
}
//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;

	case WM_KEYDOWN:
		if (wParam == VK_ESCAPE)
			::DestroyWindow(hwnd);
		break;
	}
	return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
	HINSTANCE prevInstance,
	PSTR cmdLine,
	int showCmd)
{
	if (!d3d::InitD3D(hinstance,
		d3d::WNDWIDTH, d3d::WNDHEIGHT, true, D3DDEVTYPE_HAL, &Device))
	{
		// 原版是 ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
		::MessageBox(NULL, "InitD3D() - FAILED", "Error", MB_OK);
		return 0;
	}

	if (!Setup())
	{
		// 原版是 ::MessageBox(0, "Setup() - FAILED", 0, 0);
		::MessageBox(NULL, "Setup() - FAILED", "Error", MB_OK);
		return 0;
	}
	
	d3d::EnterMsgLoop(Display);

	Cleanup();

	Device->Release();

	return 0;
}

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧