I'm trying to follow Riemer's DirectX with C++ tutorial.
I have made a few changes, for example here I have made my InitializeDevice() function in a separate file (tdirect.cpp / tdirect.h).
When I press F5 to compile and run, the program works perfectly. But when I make a change to a value (e.g. 0xff00ffff to 0xff0000ff - cyan to blue) without choosing the "Rebuild" option I get a stream of errors in my console which prevent me from compiling the program. This is pretty annoying as you can imagine. It's as if it's compiling tdirect.cpp twice.
Here's the source code of tdirect.cpp, tdirect.h, and the relevant part from main.cpp (tdirect.h is only included from main.cpp, and basicvertex.h is only included from tdirect.cpp)
tdirect.cpp:
#include "tdirect.h"
#include "basicvertex.h"
IDirect3D9 *pD3D;
D3DPRESENT_PARAMETERS D3DParams;
LPDIRECT3DDEVICE9 InitializeDevice(HWND Wnd)
{
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D == NULL)
{
MessageBox(Wnd, "DirectX is not installed.", "No DirectX!", MB_OK);
return NULL;
}
ZeroMemory(&D3DParams, sizeof(D3DPRESENT_PARAMETERS));
D3DParams.Windowed = TRUE;
D3DParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DParams.BackBufferFormat = D3DFMT_UNKNOWN;
LPDIRECT3DDEVICE9 pDevice;
if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Wnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3DParams, &pDevice)))
{
if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, Wnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DParams, &pDevice)))
{
MessageBox(Wnd, "Failed to create reference device.", "No software device!", MB_OK);
}
else {
MessageBox(Wnd, "Falling back to software mode.", "No hardware device!", MB_OK);
}
}
return pDevice;
}
LPDIRECT3DVERTEXBUFFER9 vBuffer;
void Init(LPDIRECT3DDEVICE9 pDevice)
{
BASICVERTEX Vertices[3];
Vertices[0].x = 150;
Vertices[0].y = 100;
Vertices[0].weight = 1;
Vertices[0].colour = 0xffff0000;
Vertices[1].x = 350;
Vertices[1].y = 100;
Vertices[1].weight = 1;
Vertices[1].colour = 0xff00ff00;
Vertices[2].x = 250;
Vertices[2].y = 300;
Vertices[2].weight = 1;
Vertices[2].colour = 0xff00ffff;
if (FAILED(pDevice->CreateVertexBuffer(sizeof(BASICVERTEX)*3, 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &vBuffer, NULL)))
{
MessageBox(NULL, "Failed to create vertex buffer", "Fail", MB_OK);
}
else {
void* pVertices;
if (FAILED(vBuffer->Lock(0, sizeof(BASICVERTEX)*3, (void**)&pVertices, 0)))
{
MessageBox(NULL, "Failed to lock vertex buffer", "Fail", MB_OK);
}
else {
memcpy(pVertices, Vertices, sizeof(BASICVERTEX)*3);
vBuffer->Unlock();
}
}
}
void DrawScene(LPDIRECT3DDEVICE9 pDevice)
{
pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
pDevice->BeginScene();
pDevice->SetStreamSource(0, vBuffer, 0, sizeof(BASICVERTEX));
pDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
pDevice->EndScene();
pDevice->Present(NULL, NULL, NULL, NULL);
}
tdirect.h:
#pragma once
#include <Windows.h>
main.cpp include part:
#include <Windows.h>
#include "dxheader.h"
#include "tdirect.cpp"
Compiler Errors:
1>Debug\tdirect.obj : warning LNK4042: object specified more than once; extras ignored
1>main.obj : error LNK2005: "struct IDirect3DDevice9 * __cdecl InitializeDevice(struct HWND__ *)" (?InitializeDevice##YAPAUIDirect3DDevice9##PAUHWND__###Z) already defined in tdirect.obj
1>main.obj : error LNK2005: "void __cdecl Init(struct IDirect3DDevice9 *)" (?Init##YAXPAUIDirect3DDevice9###Z) already defined in tdirect.obj
1>main.obj : error LNK2005: "void __cdecl DrawScene(struct IDirect3DDevice9 *)" (?DrawScene##YAXPAUIDirect3DDevice9###Z) already defined in tdirect.obj
1>main.obj : error LNK2005: "struct IDirect3DVertexBuffer9 * vBuffer" (?vBuffer##3PAUIDirect3DVertexBuffer9##A) already defined in tdirect.obj
1>main.obj : error LNK2005: "struct _D3DPRESENT_PARAMETERS_ D3DParams" (?D3DParams##3U_D3DPRESENT_PARAMETERS_##A) already defined in tdirect.obj
1>main.obj : error LNK2005: "struct IDirect3D9 * pD3D" (?pD3D##3PAUIDirect3D9##A) already defined in tdirect.obj
1>C:\Users\me\Documents\Visual Studio 2010\Projects\DirectX\Debug\DXStuff.exe : fatal error LNK1169: one or more multiply defined symbols found
Thanks in advance :)
You're including an implementation file from your main.cpp, so it's adding the functions in again. You can't do this:
#include "tdirect.cpp"
Instead you need to create a header file with the function prototypes so you can reference them...
Do you have these defined in your "tdirect.h" (this is what main.cpp should be including)?
LPDIRECT3DDEVICE9 InitializeDevice(HWND Wnd);
etc...
Related
Basically I am trying create a simple FileSystem MiniFilter Driver where I can modify a notepad file from writing. Following this tutorial. So I created a project in visual studio which is type Filter Driver: NDIS. here is the full code:
/*++
Module Name:
Filter.c
Abstract:
Sample NDIS Lightweight filter driver
--*/
#include "precomp.h"
PFLT_FILTER FilterHandle = NULL;
NTSTATUS MiniUnload(FLT_FILTER_UNLOAD_FLAGS Flags);
FLT_POSTOP_CALLBACK_STATUS MiniPostCreate(PFLT_CALLBACK_DATA Data, PCFLT_RELATED_OBJECTS FltObjects, PVOID* CompletionContext, FLT_POST_OPERATION_FLAGS flags);
FLT_PREOP_CALLBACK_STATUS MiniPreCreate(PFLT_CALLBACK_DATA Data, PCFLT_RELATED_OBJECTS FltObjects, PVOID* CompletionContext);
FLT_PREOP_CALLBACK_STATUS MiniPreWrite(PFLT_CALLBACK_DATA Data, PCFLT_RELATED_OBJECTS FltObjects, PVOID* CompletionContext);
const FLT_OPERATION_REGISTRATION Callbacks[] = {
{IRP_MJ_CREATE,0,MiniPreCreate,MiniPostCreate},
{IRP_MJ_WRITE,0,MiniPreCreate,NULL},
{IRP_MJ_OPERATION_END}
};
const FLT_REGISTRATION FilterRegistration = {
sizeof(FLT_REGISTRATION),
FLT_REGISTRATION_VERSION,
0,
NULL,
Callbacks,
MiniUnload,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
NTSTATUS MiniUnload(FLT_FILTER_UNLOAD_FLAGS Flags) {
KdPrint(("driver unload \r\n"));
FltUnregisterFilter(FilterHandle);
return STATUS_SUCCESS;
}
FLT_POSTOP_CALLBACK_STATUS MiniPostCreate(PFLT_CALLBACK_DATA Data, PCFLT_RELATED_OBJECTS FltObjects, PVOID* CompletionContext, FLT_POST_OPERATION_FLAGS flags) {
KdPrint(("Post Create is running \r\n"));
return FLT_POSTOP_FINISHED_PROCESSING;
}
FLT_PREOP_CALLBACK_STATUS MiniPreCreate(PFLT_CALLBACK_DATA Data, PCFLT_RELATED_OBJECTS FltObjects, PVOID* CompletionContext) {
PFLT_FILE_NAME_INFORMATION FileNameInfo;
NTSTATUS status;
WCHAR Name[300] = { 0 };
status = FltGetFileNameInformation(Data, FLT_FILE_NAME_NORMALIZED | FLT_FILE_NAME_QUERY_DEFAULT, &FileNameInfo);
if (NT_SUCCESS(status)) {
status = FltParseFileNameInformation(FileNameInfo);
if (NT_SUCCESS(status)) {
if (FileNameInfo->Name.MaximumLength < 260) {
RtlCopyMemory(Name, FileNameInfo->Name.Buffer, FileNameInfo->Name.MaximumLength);
KdPrint(("CreateFile: %ws \r\n", Name));
}
}
FltReleaseFileNameInformation(FileNameInfo);
}
return FLT_PREOP_SUCCESS_WITH_CALLBACK;
}
FLT_PREOP_CALLBACK_STATUS MiniPreWrite(PFLT_CALLBACK_DATA Data, PCFLT_RELATED_OBJECTS FltObjects, PVOID* CompletionContext) {
PFLT_FILE_NAME_INFORMATION FileNameInfo;
NTSTATUS status;
WCHAR Name[300] = { 0 };
status = FltGetFileNameInformation(Data, FLT_FILE_NAME_NORMALIZED | FLT_FILE_NAME_QUERY_DEFAULT, &FileNameInfo);
if (NT_SUCCESS(status)) {
status = FltParseFileNameInformation(FileNameInfo);
if (NT_SUCCESS(status)) {
if (FileNameInfo->Name.MaximumLength < 260) {
RtlCopyMemory(Name, FileNameInfo->Name.Buffer, FileNameInfo->Name.MaximumLength);
_wcsupr(Name);
if (wcsstr(Name, L"OPENME.TXT") != NULL) {
KdPrint(("Write File: %ws Blocked \r\n", Name));
Data->IoStatus.Status = STATUS_INVALID_PARAMETER;
Data->IoStatus.Information = 0;
FltReleaseFileNameInformation(FileNameInfo);
return FLT_PREOP_COMPLETE;
}
KdPrint(("CreateFile: %ws \r\n", Name));
}
}
FltReleaseFileNameInformation(FileNameInfo);
}
return FLT_PREOP_SUCCESS_WITH_CALLBACK;
}
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath) {
NTSTATUS status;
status = FltRegisterFilter(DriverObject, &FilterRegistration, &FilterHandle);
if (NT_SUCCESS(status)) {
status = FltStartFiltering(FilterHandle);
if (!NT_SUCCESS(status)) {
FltUnregisterFilter(FilterHandle);
}
}
return status;
}
The header files have been to precomp.h as below:
#pragma warning(disable:4201) //nonstandard extension used : nameless struct/union
#pragma warning(disable:4100)
#include <fltKernel.h>
#include <dontuse.h>
#include <suppress.h>
#include <ndis.h>
#include <filteruser.h>
#include <ntddk.h>
#include "flt_dbg.h"
#include "filter.h"
Everything else is default.
Project configuration is Active(x64) under properties.
With all of that I am getting below errors:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol FltGetFileNameInformation referenced in function MiniPreCreate default C:\Users\Abdul\source\repos\default\default\filter.obj 1
Warning 1324 [Version] section should specify PnpLockdown=1 to prevent external apps from modifying installed driver files. default C:\Users\Abdul\source\repos\default\default\default.inf 8
Error LNK2019 unresolved external symbol FltRegisterFilter referenced in function DriverEntry default C:\Users\Abdul\source\repos\default\default\filter.obj 1
Error LNK2019 unresolved external symbol FltUnregisterFilter referenced in function MiniUnload default C:\Users\Abdul\source\repos\default\default\filter.obj 1
Error LNK2019 unresolved external symbol FltStartFiltering referenced in function DriverEntry default C:\Users\Abdul\source\repos\default\default\filter.obj 1
Error LNK2019 unresolved external symbol FltReleaseFileNameInformation referenced in function MiniPreCreate default C:\Users\Abdul\source\repos\default\default\filter.obj 1
Error LNK2019 unresolved external symbol FltParseFileNameInformation referenced in function MiniPreCreate default C:\Users\Abdul\source\repos\default\default\filter.obj 1
Error LNK2001 unresolved external symbol FilterDriverHandle default C:\Users\Abdul\source\repos\default\default\device.obj 1
Error LNK2001 unresolved external symbol FilterDriverObject default C:\Users\Abdul\source\repos\default\default\device.obj 1
Error LNK2001 unresolved external symbol NdisFilterDeviceHandle default C:\Users\Abdul\source\repos\default\default\device.obj 1
Error LNK2001 unresolved external symbol NdisDeviceObject default C:\Users\Abdul\source\repos\default\default\device.obj 1
Error LNK2001 unresolved external symbol FilterListLock default C:\Users\Abdul\source\repos\default\default\device.obj 1
Error LNK2001 unresolved external symbol FilterModuleList default C:\Users\Abdul\source\repos\default\default\device.obj 1
Error LNK1120 12 unresolved externals default C:\Users\Abdul\source\repos\default\x64\Debug\default.sys 1
Can Anyone guide on what am I doing wrong?
I ran into the same issue. For me, the problem was that the mini-filter template was not showing in the templates listing for new projects and so I had to create it from scratch and I inevitably missed something. After cross-checking the linker options against the minifilter projects provided by Microsoft as reference (check here) I realized that fltMgr.lib has to be specifically provided to the linker. In order to do that right-click on the project in the "Solution Explorer" left pane. Then go to Properties->Linker->Input->Additional Dependencies. Add $(DDK_LIB_PATH)\fltMgr.lib to the list of additional dependencies and rebuild your project!
I hope this does it for you, but like the Microsoft documentation points out LNK2019 can be caused by lots of other problems with your configuration.
My project is a c++ CLR/CLI, I have some PNG images in my Resource Files and I want to change the BackgroundImage of a System::Windows::Forms::Button when the button is clicked, my code below:
#include <Windows.h>
#include "resource.h"
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
HBITMAP hBitMap = (HBITMAP) LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_PNG2), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
Bitmap^ bitMap = Bitmap::FromHbitmap((IntPtr) hBitMap);
DeleteObject(hBitMap);
button1->BackgroundImage = (System::Drawing::Image^) bitMap;
}
Here's my resource.h:
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Disneyy.rc
//
#define IDB_PNG1 101
#define IDB_PNG2 102
#define IDB_PNG3 103
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 104
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
When compiling I'm getting this error:
1>MyForm.obj : error LNK2028: unresolved token (0A000036) "extern "C" void * __stdcall LoadImageW(struct HINSTANCE__ *,wchar_t const *,unsigned int,int,int,unsigned int)" (?LoadImageW##$$J224YGPAXPAUHINSTANCE__##PB_WIHHI#Z) referenced in function "private: void __clrcall Disneyy::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click#MyForm#Disneyy##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
1>MyForm.obj : error LNK2028: unresolved token (0A00003F) "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject##$$J14YGHPAX#Z) referenced in function "private: void __clrcall Disneyy::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click#MyForm#Disneyy##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
1>MyForm.obj : error LNK2019: unresolved external symbol "extern "C" void * __stdcall LoadImageW(struct HINSTANCE__ *,wchar_t const *,unsigned int,int,int,unsigned int)" (?LoadImageW##$$J224YGPAXPAUHINSTANCE__##PB_WIHHI#Z) referenced in function "private: void __clrcall Disneyy::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click#MyForm#Disneyy##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
1>MyForm.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject##$$J14YGHPAX#Z) referenced in function "private: void __clrcall Disneyy::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click#MyForm#Disneyy##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
EDIT:
Ok I fixed it by adding User32.lib to Additional Dependencies also specifying the Entry Point as main. However there's something wrong with this line:
HBITMAP hBitMap = (HBITMAP) LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_PNG2), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
Now I'm getting this error:
First-chance exception at 0x77130c3f in Disneyy.exe: 0xC0000005: Access violation reading location 0x00000066.
A first chance exception of type 'System.AccessViolationException' occurred in Disneyy.exe
An unhandled exception of type 'System.AccessViolationException' occurred in Disneyy.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
EDIT:
Once again I fixed it by changing IMAGE_BITMAP to BI_PNG, but now a new error comes up at line: Bitmap^ bitMap = Bitmap::FromHbitmap((IntPtr) hBitMap);
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.
Finally after hours and hours of research I found an adapted solution which works just fine!
This solution is a combination of Giuseppe Pischedda's answer: https://www.codeproject.com/Articles/17996/Load-JPEG-images-from-DLL-with-LoadResource-in-Man
and Bordon's answer: https://social.msdn.microsoft.com/Forums/de-DE/c8dea6f9-0564-49d6-9782-117207031140/loadfromresource-fails-for-loading-png-files-from-resource?forum=vcmfcatl
adapted by me for PNG images:
public: System::Drawing::Image^ getImageFromRes(long resource_ID) {
// Function getImageFromRes
// A function for loading PNG images from resources in C++ CLR/CLI
// Copyright (C) Giuseppe Pischedda 2007 for most code
// and a little part of this code by Bordon and adapted by me for PNG images in C++ CLR/CLI.
//Load the resource module:
HMODULE hInst = NULL;
// Find the resource using the resource ID from file "resource.h"
HRSRC hResource = ::FindResource(hInst, MAKEINTRESOURCE(resource_ID), L"PNG");
if (!hResource) return nullptr;
// Load the resource and save the total size.
DWORD Size = SizeofResource(hInst, hResource);
HGLOBAL MemoryHandle = LoadResource(hInst, hResource);
if (MemoryHandle == NULL) return nullptr;
//Create a cli::array of byte with size = total size + 2
cli::array<BYTE>^ MemPtr = gcnew array<BYTE>(Size + 2);
//Cast from LPVOID to char *
char *lkr = (char *) (LockResource(MemoryHandle));
//Copy from unmanaged memory to managed array
System::Runtime::InteropServices::Marshal::Copy((IntPtr) lkr, MemPtr, 0, Size);
// Create a new MemoryStream with size = MemPtr
System::IO::MemoryStream^ stream = gcnew System::IO::MemoryStream(MemPtr);
//Write in the MemoryStream
stream->Write(MemPtr, 0, Size);
//Set the position for read the stream
stream->Position = 0;
//Free allocated resources
FreeLibrary(hInst);
//Create an Image abstract class pointer
System::Drawing::Image^ ptrPNG;
//Assign the stream to abstract class pointer
ptrPNG = System::Drawing::Image::FromStream(stream);
return ptrPNG;
}
Usage:
System::Drawing::Image^ image = getImageFromRes(IDB_PNG2);
if (image != nullptr) button1->BackgroundImage = image;
Iam modding in the source Engine and iam trying to add adobe Flash into it
i have already included header files and the libs from Microsoft DirectX SDK (June 2010) in visual studio 2010 but when Compiling the project it give me errors
1>FlashManager.obj : error LNK2019: unresolved external symbol _D3DXCreateSprite#8 referenced in function "public: bool __thiscall FlashManager::RecreateTargets(struct IDirect3DDevice9 *)" (?RecreateTargets#FlashManager##QAE_NPAUIDirect3DDevice9###Z)
1>FlashManager.obj : error LNK2019: unresolved external symbol _D3DXMatrixTransformation2D#28 referenced in function "public: void __thiscall FlashManager::SceneDataHook(void)" (?SceneDataHook#FlashManager##QAEXXZ)
1>FlashManager.obj : error LNK2019: unresolved external symbol "struct IFlashDX * __cdecl GetFlashToDirectXInstance(void)" (?GetFlashToDirectXInstance##YAPAUIFlashDX##XZ) referenced in function "public: __thiscall FlashManager::FlashManager(void)" (??0FlashManager##QAE#XZ)
1>.\Release_sdk\Client.dll : fatal error LNK1120: 3 unresolved externals
How Can i fix it
linked every thing
The code
bool FlashManager::RecreateTargets(IDirect3DDevice9* pD3DDevice)
{
HRESULT hr;
int movieIndex = GetPlayingMovieIndex();
int newWidth = -1, newHeight = -1;
if (movieIndex > -1)
{
newWidth = m_movieArray[movieIndex].GetWidth();
newHeight = m_movieArray[movieIndex].GetHeight();
}
else
{
newWidth = w;
newHeight = h;
}
hr = pD3DDevice->CreateTexture(newWidth, newHeight, 1, 0, transparency_mode ? D3DFMT_A8R8G8B8 : D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &g_Texture, NULL);
if (FAILED(hr))
return false;
D3DXCreateSprite(pD3DDevice, &Sprite); //the problem
g_pD3DDevice = pD3DDevice;
if (m_flashPlayer)
m_flashPlayer->ResizePlayer(newWidth, newHeight);
for (int i = 0; i < num_textures_in_rotation; ++i)
{
hr = pD3DDevice->CreateTexture(newWidth, newHeight, 1, 0,
transparency_mode ? D3DFMT_A8R8G8B8 : D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_texturesRotation[i], NULL);
if (FAILED(hr))
return false;
}
recreatedTargets = true;
return true;
}
Link error 2
D3DXMatrixTransformation2D(&Mat, NULL, 0, &Scaling, NULL, 0, &Translation); // The problem
The Last link error
FlashManager::FlashManager()
{
engine->GetScreenSize(w, h);
// Flash init
m_flashDX = GetFlashToDirectXInstance(); //the problem
m_flashPlayer = m_flashDX->CreatePlayer(w, h);
if (!m_flashPlayer)
{
MessageBox(NULL, "Flash Player failed to initialize.", "Error", MB_OK);
abort();
}
m_playerASI = new ASInterface(m_flashPlayer);
I've written a WinUSB project for obtaining data from spectrometer, the code seems to work few weeks ago. In the later stage I tried to link this project with CUDA, after few trials I solved the CUDA linker error. Unfortunately I ended up "error LNK2019: unresolved external symbol" in my main program(Winusb project). At first I thought it was because of the .cu files and I decided to remove all the .CU(CUDA)files form the project and still I was keep on getting the same error(LNK2019).
The following is my code for Winusb project (which was working perfectly few weeks back, but now I'm completely lost and needed some help)
Main.cpp
#include "pch.h"
#include <cstdio>
LONG __cdecl _tmain(LONG Argc, LPTSTR * Argv )
{
FILE *output_file1 = fopen("output_file2.txt", "w");
//FILE *output_file2 = fopen("output_file3.txt", "w");
DEVICE_DATA deviceData;
HRESULT hr;
USB_DEVICE_DESCRIPTOR deviceDesc;
BOOL bResult;
BOOL noDevice;
ULONG lengthReceived;
BOOL wrResult = TRUE;
BOOL wr1Result = TRUE;
BOOL RQResult = 0;
UNREFERENCED_PARAMETER(Argc);
UNREFERENCED_PARAMETER(Argv);
//////////////////////Open device ///////////////
hr = OpenDevice(&deviceData, &noDevice);
if (FAILED(hr)) {
if (noDevice) {
printf(_T("Device not connected or driver not installed\n"));
} else {
printf(_T("Failed looking for device, HRESULT 0x%x\n"), hr);
}
std::getchar();
return 0;
}
/////////////////////Get descriptor//////////////////
bResult = WinUsb_GetDescriptor(deviceData.WinusbHandle,
USB_DEVICE_DESCRIPTOR_TYPE,
0,
0,
(PBYTE) &deviceDesc,
sizeof(deviceDesc),
&lengthReceived);
if (FALSE == bResult || lengthReceived != sizeof(deviceDesc)) {
printf(_T("Error among LastError %d or lengthReceived %d\n"),
FALSE == bResult ? GetLastError() : 0,
lengthReceived);
CloseDevice(&deviceData);
return 0;
}
bool sResult = FALSE;bool syResult;
bool sResult1 = FALSE;bool syResult1;
//Initialize
UCHAR Intialize[] = {0x01};
ULONG cbISize = strlen((char*)Intialize);
ULONG InSent = 0;
wrResult = WinUsb_WritePipe(deviceData.WinusbHandle, 0x01, Intialize, 1, &InSent, 0);
//Integration time - 700ms
UCHAR Inttime[] = {0x0200100000};
ULONG cbITSize = strlen((char*)Inttime);
ULONG InttimeSent = 0;
wrResult = WinUsb_WritePipe(deviceData.WinusbHandle, 0x01, Inttime, 5, &InttimeSent, 0);
//strobe signal
UCHAR strobe1[] = {0x030001};
ULONG strobeSize1 = strlen((char*)strobe1);
ULONG strobeSent1 = 0;
wr1Result = WinUsb_WritePipe(deviceData.WinusbHandle, 0x01, strobe1, 3, &strobeSent1, 0);
//Request spectra
UCHAR Rqspectra[] = {0x09};
ULONG RqSize = strlen((char*)Rqspectra);
ULONG RqSent = 0;
RQResult = WinUsb_WritePipe(deviceData.WinusbHandle, 0x01, Rqspectra,1, &RqSent, 0);
//Pixel Values
UCHAR szBuffer[15][512];
UCHAR sz1Buffer[1];
UCHAR tBuffer[1];
ULONG tReadx;
ULONG cbReadx[16];
USHORT newbuf[15][512];
short specbu[7860];
for (int i=0;i<16;i++)
{
if (i<4)
{
sResult = WinUsb_ReadPipe(deviceData.WinusbHandle, 0x86, szBuffer[i], 512, &cbReadx[i], 0);
}
else if (i>=4 && i<15)
{
sResult = WinUsb_ReadPipe(deviceData.WinusbHandle, 0x82, szBuffer[i], 512, &cbReadx[i], 0);
}
else if (i = 15)
{
syResult = WinUsb_ReadPipe(deviceData.WinusbHandle, 0x82, sz1Buffer, 1, &cbReadx[i], 0);
}
}
int pon=0;
for (int k=0;k<15;k++)
{
for (int l=0;l<512;l+=2)
{
newbuf[k][l] = (szBuffer[k][(l+1)]<<8|szBuffer[k][l]);
specbu[pon]= (szBuffer[k][(l+1)]<<8|szBuffer[k][l]);
fprintf(output_file1,"%d\t\n",specbu[pon]);
pon++;
}
}
std::getchar();
CloseDevice(&deviceData);
return 0;
}
My Pch.h
#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <winusb.h>
#include <usb.h>
#include "device.h"
My device.h
#include
//
// Device Interface GUID.
// Used by all WinUsb devices that this application talks to.
// Must match "DeviceInterfaceGUIDs" registry value specified in the INF file.
// 390a138c-f867-4538-8fd4-46063b842d2b
//
DEFINE_GUID(GUID_DEVINTERFACE_USBApplication2,
0x390a138c,0xf867,0x4538,0x8f,0xd4,0x46,0x06,0x3b,0x84,0x2d,0x2b);
typedef struct _DEVICE_DATA {
BOOL HandlesOpen;
WINUSB_INTERFACE_HANDLE WinusbHandle;
HANDLE DeviceHandle;
TCHAR DevicePath[MAX_PATH];
}
DEVICE_DATA, *PDEVICE_DATA;
HRESULT
OpenDevice(
_Out_ PDEVICE_DATA DeviceData,
_Out_opt_ PBOOL FailureDeviceNotFound
);
VOID
CloseDevice(
_Inout_ PDEVICE_DATA DeviceData
);
My build log
1>------ Build started: Project: USB Application2, Configuration: Win7 Debug Win32 ------
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol "long __stdcall OpenDevice(struct _DEVICE_DATA *,int *)" (?OpenDevice##YGJPAU_DEVICE_DATA##PAH#Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "void __stdcall CloseDevice(struct _DEVICE_DATA *)" (?CloseDevice##YGXPAU_DEVICE_DATA###Z) referenced in function _main
1>C:\Users\bel1\Documents\Visual Studio 2012\Projects\USB Application2\Win7Debug\USBApplication2.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Deploy: 0 succeeded, 0 failed, 0 skipped ==========
You will need to add source files (.CPP) into your project that are having implementation of following functions:
OpenDevice
CloseDevice
If you are using a third party library, you need to put the relevant .LIB file into Linker Input of project settings.
I'm trying to convert a bmp image to a png one with this code:
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_DEPRECATE
#include <png.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
void GetDesktopResolution(int& horizontal, int& vertical)
{
RECT desktop;
// Get a handle to the desktop window
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
// The top left corner will have coordinates (0,0)
// and the bottom right corner will have coordinates
// (horizontal, vertical)
horizontal = desktop.right;
vertical = desktop.bottom;
}
typedef struct _RGBPixel {
uint8_t blue;
uint8_t green;
uint8_t red;
} RGBPixel;
/* Structure for containing decompressed bitmaps. */
typedef struct _RGBBitmap {
RGBPixel *pixels;
size_t width;
size_t height;
size_t bytewidth;
uint8_t bytes_per_pixel;
} RGBBitmap;
/* Returns pixel of bitmap at given point. */
#define RGBPixelAtPoint(image, x, y) \
*(((image)->pixels) + (((image)->bytewidth * (y)) \
+ ((x) * (image)->bytes_per_pixel)))
/* Attempts to save PNG to file; returns 0 on success, non-zero on error. */
int save_png_to_file(RGBBitmap *bitmap, const char *path)
{
FILE *fp = fopen(path, "wb");
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_uint_32 bytes_per_row;
png_byte **row_pointers = NULL;
if (fp == NULL) return -1;
/* Initialize the write struct. */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
return -1;
}
/* Initialize the info struct. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
return -1;
}
/* Set up error handling. */
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return -1;
}
/* Set image attributes. */
png_set_IHDR(png_ptr,
info_ptr,
bitmap->width,
bitmap->height,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
/* Initialize rows of PNG. */
bytes_per_row = bitmap->width * bitmap->bytes_per_pixel;
png_malloc(png_ptr, bitmap->height * sizeof(png_byte *));
for (y = 0; y < bitmap->height; ++y) {
uint8_t *row = (uint8_t *)png_malloc(png_ptr, sizeof(uint8_t)* bitmap->bytes_per_pixel);
row_pointers[y] = (png_byte *)row;
for (x = 0; x < bitmap->width; ++x) {
RGBPixel color = RGBPixelAtPoint(bitmap, x, y);
*row++ = color.red;
*row++ = color.green;
*row++ = color.blue;
}
}
/* Actually write the image data. */
png_init_io(png_ptr, fp);
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
/* Cleanup. */
for (y = 0; y < bitmap->height; y++) {
png_free(png_ptr, row_pointers[y]);
}
png_free(png_ptr, row_pointers);
/* Finish writing. */
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return 0;
}
int main()
{
RGBBitmap rgbbitmap;
int w, h;
GetDesktopResolution(w, h);
rgbbitmap.height = h;
rgbbitmap.width = w;
rgbbitmap.bytes_per_pixel = 1;
rgbbitmap.bytewidth = w / 100;
RGBPixel rgbpixel;
rgbpixel.blue = 100;
rgbpixel.green = 100;
rgbpixel.red = 100;
rgbbitmap.pixels = &rgbpixel;
save_png_to_file(&rgbbitmap, "abc.bmp");
return 0;
}
Executing this code triggers these errors :
LNK1120: 9 unresolved externals
LNK2019: unresolved external symbol _png_create_info_struct referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
LNK2019: unresolved external symbol _png_create_write_struct referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
LNK2019: unresolved external symbol _png_destroy_write_struct referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
LNK2019: unresolved external symbol _png_free referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
LNK2019: unresolved external symbol _png_init_io referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
LNK2019: unresolved external symbol _png_malloc referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
LNK2019: unresolved external symbol _png_set_IHDR referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
LNK2019: unresolved external symbol _png_set_rows referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
LNK2019: unresolved external symbol _png_write_png referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" (?save_png_to_file##YAHPAU_RGBBitmap##PBD#Z)
I'm not able to find how to fix these errors. Any brilliant suggestion, please?
I'm currently using Visual Studio Ultimate 2013 on a Windows 7 SP1 plateform.
Thanks a lot!
I think, you didn't linked your libraries, just included headers. This question anwers how you do it...
If not, there is plently of what can happen:
you are trying to call those functions with bad parameters
you included bad header file
you have mixed libraries or you are trying to link MinGW, VS2012/VS2012 compiled library to VS2013 compiler, since i don't know if they are compatible...
You can try to download png library, create VS2012 project and try to compile it. When you do, you should absolutely have no problems while linking...