Using a win32 DLL library on a Universal Windows App C++ - c++

I was able to compile a dll library based by following the instructions indicated from this tutorial http://tutplusplus.blogspot.com/2011/04/c-tutorial-create-dll-in-vc-20102008.html. I compiled it and there are no problems. I also tested my dll library on an empty win32 console application solution and it compiled fine by adding the header, source file and changing the linker settings. The problem is I can't use the dll library this time on a Universal Windows App solution. I added the header and source files as well as changed the linker settings.
When I compiled the project solution it asked me to include pch.h to the source file of my library so I added the line #include "pch.h" on top.
After that, other compiler errors came up.
'CreateFile': identifier not found and 'SetCommState': identifier not found
I believe the compiler for my current project solution recompiled everything again without considering the dll library's compilation. How can I make the project solution just compile its own source and header files?
Lib Header:
#pragma once
#include <Windows.h>
namespace nmspace
{
class SerialPort
{
public:
static __declspec(dllexport) int connect();
};
}
Lib Source:
#include <iostream>
using namespace std;
#include "SimpleH.h"
#include <Windows.h>
namespace nmspace
{
int SerialPort::connect()
{
int error = 0;
DCB dcb;
memset(&dcb, 0, sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 57600;
dcb.Parity = NOPARITY;
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
HANDLE m_serialPortHandle = CreateFile(L"COM7", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL);
if (m_serialPortHandle != INVALID_HANDLE_VALUE) {
if (!SetCommState(m_serialPortHandle, &dcb))
error = 2;
}
else
error = 1;
return error;
}
}

You won't be able to use this DLL because it uses SetCommState function that is only available for desktop applications. You should lookup functions that you are trying to use in a list of desktop APIs available for UWP applications.
As for CreateFile it is not a function, but a macro expanding to CreateFileA or CreateFileW depending on project Unicode settings. You should use CreateFile2 instead which is available for both Desktop and UWP applications.

The reason is very simple. WinRT doesn't support the complete WinApi! You must use the WinRT function from the UWP Namespace
Just read the docs to CreateFile. In the documentations you find under requirements:
Minimum supported client Windows XP [desktop apps only] Minimum
supported server Windows Server 2003 [desktop apps only]
This tells you that CreateFile isn't allowed in WinRT.
AFAIK accessing a serial port from WinRT isn't possible. Just google. Here is an answer that states this. Same here

Related

Visual Studio - Create exe inclusive of ssh.dll

I have built a simple program in Visual Studio, as per the below.
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <iostream>
#include <stdlib.h>
int main()
{
std::cout << "Hello World!\n";
ssh_session my_ssh_session;
int verbosity = SSH_LOG_PROTOCOL;
int port = 22;
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "10.10.10.100");
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_free(my_ssh_session);
}
For the Lisssh library, I have downloaded the code from here:
https://github.com/ShiftMediaProject/libssh/releases
...and added the library to Visual Studio, following the instructions here
How to manually add a library to a visual studio 2017 project?
The library works and the program compiles, my only issue is that in order for it to work I have to manually copy a file called ssh.dll into the folder where the program executes from.
I have tried in Visual Studio setting C/C++, Code Generation -> Multi-threaded (/MT) to statically link the library, but the DLL is not getting included.
How can I create a single executable, that includes the ssh.dll?
Thanks
A .dll is a Dynamic Link Library; it's not meant to be included in an .exe. There are ways, but they are ugly and difficult.
The "proper" and easier way is to build libssh as a static library instead; this will give you a .lib file that you can add to your .exe easily.
It looks like the downloads you used already contain these .lib files. Just add the libssh.lib file to your project, and omit the ssh.lib (which is just an import library for the .dll, as you can tell from the size of the file).

Error Loading Enclave: Couldn't open file with CreateFile()

I'm trying to write a simple SGX project for a start. So I have this main host application routine that I've pretty much copied from Lars Richter's blog:
#define ENCLAVE_FILE _T("Enclave.signed.dll")
#include <tchar.h>
#include <cstdio>
#include "sgx_urts.h"
#include "Enclave_u.h"
int main()
{
sgx_enclave_id_t eid;
sgx_status_t ret = SGX_SUCCESS;
sgx_launch_token_t token = { 0 };
int updated = 0;
ret = sgx_create_enclave(ENCLAVE_FILE, SGX_DEBUG_FLAG, &token, &updated, &eid, NULL);
if (ret != SGX_SUCCESS) {
printf("\nApp: error %#x, failed to create enclave.\n", ret);
}
scanf("\n");
return 0;
}
It compiles fine (I'm using the Intel C++ 17.0 compiler with Visual Studio 2015) but it doesn't load the enclave. I get the following error message:
[sgx_create_enclavew ..\urts\win\urts.cpp:195] Couldn't open file with CreateFile()
App: error 0x200f, failed to create enclave.
Go to app_test_save project setting. Under Debugging, change working directory to $(SolutionDir)Debug. This answer assumes that both projects app_test_save and enclave_test_save belong to the same solution.
As Neil pointed out, sgx_create_enclave couldn't find the dll when the program was being run from within Visual Studio's debugger. It worked fine when I directly ran the executable in the "Debug" folder.
So a simple trick to make it work in both settings is to do the following:
#define ENCLAVE_FILE _T("../Debug/Enclave.signed.dll")
According to this : https://software.intel.com/en-us/forums/intel-software-guard-extensions-intel-sgx/topic/623738
If you are using the Local SGX Debugger, Please make sure change the "current working directory" pointing to $(OutDir) instead of $(ProjectDir).
Configuration Properties --> Debugging --> Working Directory --> $(OutDir).
Error is basically means it could not locate your .dll file.
Do dir /a/s to find Enclave.signed.dll then change the name appropriately.
When you create enclave it will generate signed.dll file. If your enclave name is Enclave12 then the DLL name is Enclave12.signed.dll. You fix this then you should be good to go.

Windows API function not available while it should be

I'm trying to create a symbolic link using Windows's API, however the CreateSymbolicLink function doesn't seem to exist even though I've included windows.h.
I'm using Qt Creator. I am able to use other functions from the API except this one.
Can anyone explain why?
I'm on Windows 8.1.
Edit:
My code:
#include "windows.h"
bool createSymbolicLink(const int &type, const QString &linkPath, const QString &targetPath) {
DWORD windows_Type = type;
wchar_t* windows_LinkPath = new wchar_t[linkPath.length()];
linkPath.toWCharArray(windows_LinkPath);
wchar_t* windows_TargetPath = new wchar_t[targetPath.length()];
targetPath.toWCharArray(windows_TargetPath);
return CreateSymbolicLinkW(windows_LinkPath, windows_TargetPath, windows_Type) != 0;
}
The error: 'CreateSymbolicLinkW' was not declared in this scope
CreateSymbolicLink has a minimum requirement of Vista, as documented at MSDN.
This means that you need to specify by way of a conditional define that you are targeting Vista and up. You can find more documentation here:
Using the Windows Headers.
Modifying WINVER and _WIN32_WINNT.
So, you could include these defines before you include the windows header file.
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
If this does not help then you have out of date SDK and will need to switch to a more up to date SDK.

c++ file datestamp change by ms sql xp called by trigger to allow refresh vb.net forms application

I'm using SQL2012 and VS2012.
I'd like to implement this solution for watching a file's date and time stamp which is updated by a dll when a trigger on a SQL table is fired.
Here is what I'm trying to do.
Which references a MSDN article
I’ve followed the instructions but got this:
Msg 17750, Level 16, State 0, Procedure xsp_UpdateSignalFile, Line 1
Could not load the DLL XSP.dll, or one of the DLLs it references. Reason: 193(%1 is not a valid Win32 application.).
I’m assuming because I’m using SQL2012 64-bit.
I downloaded the XSP.DLL source and looking at in the VS2012 but can’t get it to compile, even after finding myself a copy of opends60.lib
Error 4 error LNK2019: unresolved external symbol _srv_rpcparams referenced in function _xsp_UpdateSignalFile C:\Users\Administrator\Documents\xsp.dll\WickedCode0304\XSP\XSP.obj XSP
Can a C++ wizard help me compile this to 64bit or am I going wrong earlier?
It is saying it can't resolve some received params?
The .cpp file looks like this:
#include <windows.h>
#include <srv.h>
///////////////////////////////////////////////////////////////////////
// Entry point
extern "C" BOOL WINAPI DllMain (HINSTANCE hInstance, DWORD dwReason,
LPVOID lpReserved)
{
return TRUE;
}
///////////////////////////////////////////////////////////////////////
// Exported functions
extern "C" __declspec (dllexport)
ULONG __GetXpVersion ()
{
return ODS_VERSION;
}
extern "C" __declspec (dllexport)
SRVRETCODE xsp_UpdateSignalFile (SRV_PROC *srvproc)
{
//
// Make sure an input parameter is present.
//
if (srv_rpcparams (srvproc) == 0)
return -1;
//
// Extract the file name from the input parameter.
//
BYTE bType;
char file[256];
ULONG ulMaxLen = sizeof (file);
ULONG ulActualLen;
BOOL fNull;
if (srv_paraminfo (srvproc, 1, &bType, &ulMaxLen, &ulActualLen,
(BYTE*) file, &fNull) == FAIL)
return -1;
if (bType != SRVBIGCHAR && bType != SRVBIGVARCHAR)
return -1;
file[ulActualLen] = 0;
//
// Update the file's time stamp.
//
char path[288] = "C:\\AspNetSql\\";
lstrcat (path, file);
HANDLE hFile = CreateFile (path, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle (hFile);
return 0;
}
I got this working with using SQL Server 2008 SP3 Dev 64-bit and VS 2012 Ultimate running on Win 7 Pro 64-bit. Here's what I did:
Created a new Empty Project from the VC++templates in VS and added an empty C++ file.
Changed platform to x64 in the Configuration manager.
Changed the Configuration Type to Dynamic library (.dll) under Configuration Properties/General.
Installed the SQL x64 native client including the SQL Server Native Client SDK to get the opends60.lib. I'm not sure the SDK component is actually needed.
Added C:\Program Files\Microsoft SQL Server\90\SDK\Lib\x64 to Additional library directories in the Linker options. This was were the native client was installed.
Added opends60.lib to Additional dependencies in the Linker options.
Compiled without errors.
Created the output directory that the XSP.dll uses: C:\AspNetSql\.
Copied the XSP.dll to the directory created in step 8.
Installed the extended stored procedure using:
USE master
EXEC sp_addextendedproc 'xsp_UpdateSignalFile', 'C:\AspNetSql\XSP.dll'
GRANT EXECUTE ON xsp_UpdateSignalFile TO PUBLIC
Executed it using master..xsp_UpdateSignalFile 'Quotes.Quotations'and verified it worked by confirming that the file Quotes.Quotations was created in the output directory.
If you have any questions or want clarification on any step I can add screenshots or provide more information.

Visual C++ code not working in Code::Blocks

I have the following code that I am currently using to call functions from a C# Dll, which works perfectly in Visual C++.
#include <mscoree.h>
#include <stdio.h>
#pragma comment(lib, "mscoree.lib")
void Bootstrap()
{
ICLRRuntimeHost *pHost = NULL;
HRESULT hr = CorBindToRuntimeEx(L"v4.0.30319", L"wks", 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*)&pHost);
pHost->Start();
printf("HRESULT:%x\n", hr);
// target method MUST be static int method(string arg)
DWORD dwRet = 0;
hr = pHost->ExecuteInDefaultAppDomain(L"c:\\temp\\test.dll", L"Test.Hello", L"SayHello", L"Person!", &dwRet);
printf("HRESULT:%x\n", hr);
hr = pHost->Stop();
printf("HRESULT:%x\n", hr);
pHost->Release();
}
int main()
{
Bootstrap();
}
The problem is, when I move this into Code::Blocks (which I am more familiar with - as the little C++ I have done has been native) throws a lot of compiler errors.
The original compiler errors were because it couldn't find the header mscoree.h. I found this in the .NET SDK, so I copied it over to the mingw include directory which solved that, and then I did the same for all the other headers it couldn't find.
After copying over all the headers it then started giving a whole load of other errors, to do with the code in the headers I had just moved - nothing to do with the code below.
Why is Code::Blocks having such a hard time running this when VS runs it straight off the bat?
Thanks
Code::Blocks is a great IDE for C++ programming, but you are clearly doing Windows programming here. Though it is the same programming language, compilers are not compatible among them.
Either if you have downloaded the CodeBlocks version with the gcc compiler, or the single CodeBlocks IDE, you need to configure CodeBlocks in order to use the MS C++ compiler. In order to do that, go to Settings >> Compiler and debugger >> Toolchain executables.
Also, in the same option, look for Search directories and place there the path to the MS C++ compiler headers.
Once that is done, you will be able to compile your program.
Code::Blocks has a different compiler altogether from Visual Studio, the decoding and encoding on source code during compilation is different and cannot recognize each other though they are same programming language.