DLL access from Excel 2016 32 Bit get "Anwendungfehler49" - c++

I get a "Anwendungsfehler 49" calling 32Bit version of dll from 32 Bit Excel 2016. Excel64 Bit with 64 Bit version of dll works fine. The Messagebox in the dll in shown but after that in the return the error occurs. Has anybody an idea for that?
Here is my dll code:
// dllmain.cpp : Definiert den Einstiegspunkt für die DLL-Anwendung.
#include "pch.h"
#include <iostream>
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
TEST_API long myTestFunction(LPCSTR test)
{
size_t origsize = strlen(test) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, test, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
MessageBox(NULL, L"Open the message box ", wcstring, MB_OK | MB_SYSTEMMODAL);
return (long) 42;
}
// pch.h:
#ifndef PCH_H
#define PCH_H
// Fügen Sie hier Header hinzu, die vorkompiliert werden sollen.
#include "framework.h"
#endif //PCH_H
#if defined(_MSC_VER)
#include <windows.h>
#define TEST_API extern "C" __declspec(dllexport)
#else
#define TEST_API
#endif
TEST_API long myTestFunction(LPCSTR test);
Excel vba code:
#If Win64 Then
Private Declare PtrSafe Function myTestFunction Lib "MyTestDll.dll" (ByVal parameter As String) As Long
#Else
Private Declare Function myTestFunction Lib "MyTestDll32.dll" (ByVal parameter As String) As Long
#End If
Sub Test()
Dim Result As Long
Result = myTestFunction("This is a test!")
End Sub

Related

DLL Injection into notepad

I want to make a message box appear in notepad , so I found a simple dll injection example. The injector itself is not mine and seems to work fine (gets the process's id , creates a remote thread , gets absolute path of the dll file). The problem, I think, is in the implementation of the dll. The projects compile without any warnings, but the expected outcome isn't achieved. Can you take a look and help me understand the problem? (I have put the release version of the dll in the injector project folder)
dllmain.cpp:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
DLLEXPORT void mess() {
MessageBoxA(NULL, "HELLO THERE", "From Notepad", NULL);
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH: mess(); break;
case DLL_THREAD_ATTACH: mess(); break;
case DLL_THREAD_DETACH: mess(); break;
case DLL_PROCESS_DETACH: mess(); break;
}
return TRUE;
}
dll.h:
#ifndef _DLL_H_
#define _DLL_H_
# define DLLEXPORT __declspec (dllexport)
# define DLLIMPORT __declspec (dllimport)
DLLEXPORT void mess(void);
#endif
and the injection.cpp for reference, it contains a function which finds the wanted process id, a function which creates the remote thread and a main:
#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <shlwapi.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define WIN32_LEAN_AND_MEAN
#define CREATE_THREAD_ACCESS (PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ)
DWORD GetProcessId(IN PCHAR szExeName)
{
DWORD dwRet = 0;
DWORD dwCount = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe = { 0 };
pe.dwSize = sizeof(PROCESSENTRY32);
BOOL bRet = Process32First(hSnapshot, &pe);
while (bRet)
{
if (!strcmp( szExeName, pe.szExeFile))
{
dwCount++;
dwRet = pe.th32ProcessID;
}
bRet = Process32Next(hSnapshot, &pe);
}
if (dwCount > 1)
dwRet = 0xFFFFFFFF;
CloseHandle(hSnapshot);
}
return dwRet;
}
BOOL CreateRemoteThreadInject(DWORD ID, const char * dll)
{
HANDLE Process;
LPVOID Memory;
LPVOID LoadLibrary;
if (!ID) return false;
Process = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, ID);
LoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
Memory = (LPVOID)VirtualAllocEx(Process, NULL, strlen(dll) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(Process, (LPVOID)Memory, dll, strlen(dll) + 1, NULL);
CreateRemoteThread(Process, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibrary, (LPVOID)Memory, NULL, NULL);
CloseHandle(Process);
VirtualFreeEx(Process, (LPVOID)Memory, 0, MEM_RELEASE);
return true;
}
int main()
{
char dll[MAX_PATH] ;
GetFullPathName("testdll.dll", MAX_PATH, dll, NULL);
DWORD ID = GetProcessId("notepad.exe");
if (!CreateRemoteThreadInject(ID, dll)) cout<<"failure";
else cout << "success";
return 0;
}
Thanks.
Be carefull on x64 x86 binaries
On windows 7 / 8 / 10 notepad.exe is a 64 bits process, so you need to compile your DLL & injector in x64

Open NFC - Error LNK2019: unresolved external symbol __imp__ShellExecuteW#24

I have an error and I don´t have idea what´s the problem.
I'm trying to compile one example of Open NFC for a project. The example is test_ndef_url. I do not know if the problem to a coding error or a library of windows is due, shellapi.h.
Test_ndef_url code is:
/*
* Copyright (c) 2007-2012 Inside Secure, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*******************************************************************************
Implementation of the Example 1 for Open NFC.
*******************************************************************************/
/* Allow use of features specific to Windows XP or later. */
#ifndef _WIN32_WINNT
/* Change this to the appropriate value to target other versions of Windows. */
#define _WIN32_WINNT 0x0601
#endif
/* Suppress many files from the compilation */
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#include "shellapi.h"
#include <stdio.h>
#include <stdlib.h>
#include "open_nfc.h"
#include "win32_test_core.h"
/* Type definititions */
/* ------------------ */
/* Local prototypes */
/* ---------------- */
/* Global variables */
/* ---------------- */
struct __testContext
{
char16_t* pURL;
bool_t bVirtualTag;
bool_t bReadTest;
W_HANDLE hRegistry;
} ;
struct __testContext g_testCtx = { null, W_FALSE, W_FALSE, W_NULL_HANDLE };
/* Callback function of type tWBasicGenericHandleCallbackFunction */
static void ReadMessageOnAnyTagCompletedURL(void *pCallbackParameter, W_HANDLE hMessage, W_ERROR nError)
{
if(nError == W_SUCCESS)
{
W_HANDLE hRecord = WNDEFGetRecord(hMessage, 0);
WBasicCloseHandle(hMessage);
if(WRTDIsURIRecord(hRecord))
{
char16_t aURIValue[256];
if(WRTDURIGetValue(hRecord, (char16_t*)&aURIValue, 256) == W_SUCCESS)
{
printf("The URL read in the tag is \"%S\"\n", aURIValue);
ShellExecute(NULL, L"open", (char16_t*)aURIValue, NULL, NULL, SW_SHOWNORMAL);
}
}
else
{
printf("Error URL: Error detected in the tag message.\n");
}
WBasicCloseHandle(hRecord);
}
else if(nError == W_ERROR_ITEM_NOT_FOUND)
{
printf("Error URL: This tag does not contain a message of the right type.\n");
}
else if(nError == W_ERROR_TIMEOUT)
{
printf("Error URL: Lost the communication with the tag.\n");
}
else
{
printf("Error URL: Error 0x%08X returned during the tag detection.\n", nError);
}
printf("Present a new tag to be read or press Ctrl+C to stop the application\n");
}
/* Callback function of type tWBasicGenericCallbackFunction */
static void WriteMessageOnAnyTagCompleted(void *pCallbackParameter, W_ERROR nError)
{
switch(nError)
{
case W_SUCCESS:
printf("The message is written in the tag.\n");
break;
case W_ERROR_LOCKED_TAG:
printf("The operation failed because the tag is locked.\n");
break;
case W_ERROR_TAG_FULL:
printf("The message is too large for the remaining free space in the tag.\n");
break;
default:
printf("An error occured during the writing operation (code 0x%x)\n", nError);
break;
}
StopApplication();
}
/* Receive the event of the virtual tag */
static void VirtualTagEventHandler(void *pCallbackParameter, uint32_t nEventCode)
{
switch(nEventCode)
{
case W_VIRTUAL_TAG_EVENT_SELECTION:
printf("The tag is selected by the reader.\n");
break;
case W_VIRTUAL_TAG_EVENT_READER_LEFT:
printf("The reader left the tag without reading the content.\n");
break;
case W_VIRTUAL_TAG_EVENT_READER_READ_ONLY :
printf("The reader read the tag.\nPresent again a reader to read the virtual tag or press Ctrl+C to stop the application\n");
break;
case W_VIRTUAL_TAG_EVENT_READER_WRITE:
default:
printf("This event should not occur for a read-only virtual tag.\n");
break;
}
}
/**
* GetSpecificTestSyntax
*
* #return String describing the command line syntax specific to this test
**/
char16_t * GetSpecificTestSyntax( void )
{
return L"[url <URL> | virtualurl <URL>\n"
L" - If the parameter is not present, the application waits to read a tag with an URL.\n"
L" - If \"url\" is present, the application waits for a Tag to write the URL.\n"
L" - If \"virtualurl\" is present, the application simulates a tag containing the URL.\n\n"
L"The following tags are supported: Type 2, Type 4-A, Type 4-B, Type 5-2K, Type 5-32K and Type 6.\n\n" ;
}
/**
* VerifyTestConditions
*
* #param[in] nArgc number of arguments
*
* #param[in] pArgv arguments array
*
* #return W_SUCCESS Arguments syntax is correct
* W_ERROR_xxx is syntax error is detected
**/
W_ERROR VerifyTestConditions( int32_t nArgc, char16_t* pArgv[] )
{
if(nArgc == 3)
{
if(wcscmp(pArgv[1], L"url") == 0)
{
g_testCtx.pURL = pArgv[2];
}
else if(wcscmp(pArgv[1], L"virtualurl") == 0)
{
g_testCtx.bVirtualTag = W_TRUE;
g_testCtx.pURL = pArgv[2];
}
else
{
nArgc = 0;
}
}
if((nArgc != 1) && (nArgc != 3))
{
return W_ERROR_BAD_PARAMETER;
}
g_testCtx.bReadTest = (nArgc == 1) ? W_TRUE : W_FALSE ;
return W_SUCCESS;
}
/**
* LaunchTest
*
**/
W_ERROR LaunchTest( void )
{
W_HANDLE hMessage = W_NULL_HANDLE;
if(g_testCtx.bReadTest != W_FALSE)
{
WNDEFReadMessageOnAnyTag(ReadMessageOnAnyTagCompletedURL, null, W_PRIORITY_MAXIMUM,
W_NDEF_TNF_WELL_KNOWN, L"U", &g_testCtx.hRegistry);
printf("Present the tag to be read or press Ctrl+C to stop the application\n");
}
else
{
if(WNDEFCreateNewMessage(&hMessage) != W_SUCCESS)
{
printf("Error: Cannot create the new message.\n");
goto return_function;
}
if(WRTDURIAddRecord(hMessage, g_testCtx.pURL) != W_SUCCESS)
{
printf("Error: Cannot add the URI record in the message.\n");
goto return_function;
}
if(g_testCtx.bVirtualTag == W_FALSE)
{
printf("Ready to write the URL \"%S\"\n", g_testCtx.pURL);
WNDEFWriteMessageOnAnyTag(WriteMessageOnAnyTagCompleted, null, W_PRIORITY_MAXIMUM, hMessage, W_NDEF_ACTION_BIT_ERASE | W_NDEF_ACTION_BIT_FORMAT_ALL, &g_testCtx.hRegistry);
printf("Present the tag to be written or press Ctrl+C to stop the application\n");
}
else
{
W_ERROR nError;
W_HANDLE hVirtualTag;
static const uint8_t aPUPI[] = { 0x01, 0x02, 0x03, 0x04 };
printf("Ready to simulate a tag with the URL \"%S\"\n", g_testCtx.pURL);
/* Create the virtual tag, the maximum length is set to the size of the message */
nError = WVirtualTagCreate(W_PROP_NFC_TAG_TYPE_4_B, aPUPI, sizeof(aPUPI),
WNDEFGetMessageLength(hMessage), &hVirtualTag);
if(nError != W_SUCCESS)
{
printf("Cannot create the virtual tag\n");
}
else
{
/* Write the message in the virtual tag */
nError = WNDEFWriteMessageSync(hVirtualTag, hMessage, W_NDEF_ACTION_BIT_ERASE | W_NDEF_ACTION_BIT_LOCK | W_NDEF_ACTION_BIT_FORMAT_ALL);
if(nError != W_SUCCESS)
{
printf("Cannot write the message in the virtual tag\n");
WBasicCloseHandle(hVirtualTag);
}
else
{
/* Start the tag simulation */
nError = WVirtualTagStartSync(hVirtualTag, VirtualTagEventHandler, null, W_TRUE);
if(nError != W_SUCCESS)
{
printf("Cannot activate the virtual tag\n");
WBasicCloseHandle(hVirtualTag);
}
else
{
printf("Present a reader to read the virtual tag or press Ctrl+C to stop the application\n");
}
}
}
}
}
return_function:
WBasicCloseHandle(hMessage);
/* Go pending on WaitForSingleObject (Card/Tag operation completed or Ctrl-C) */
return W_SUCCESS;
}
/**
* CloseTest
*
**/
void CloseTest( void )
{
WBasicCloseHandle( g_testCtx.hRegistry );
}
The part of the code is defined shellapi.h where ShellExecute is:
#include <winapifamily.h>
/*****************************************************************************\
* *
* shellapi.h - SHELL.DLL functions, types, and definitions *
* *
* Copyright (c) Microsoft Corporation. All rights reserved. *
* *
\*****************************************************************************/
#ifndef _INC_SHELLAPI
#define _INC_SHELLAPI
#include <SpecStrings.h>
//
// Define API decoration for direct importing of DLL references.
//
#ifndef WINSHELLAPI
#if !defined(_SHELL32_)
#define WINSHELLAPI DECLSPEC_IMPORT
#else
#define WINSHELLAPI
#endif
#endif // WINSHELLAPI
#ifndef SHSTDAPI
#if !defined(_SHELL32_)
#define SHSTDAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE
#define SHSTDAPI_(type) EXTERN_C DECLSPEC_IMPORT type STDAPICALLTYPE
#else
#define SHSTDAPI STDAPI
#define SHSTDAPI_(type) STDAPI_(type)
#endif
#endif // SHSTDAPI
#ifndef SHDOCAPI
#if !defined(_SHDOCVW_)
#define SHDOCAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE
#define SHDOCAPI_(type) EXTERN_C DECLSPEC_IMPORT type STDAPICALLTYPE
#else
#define SHDOCAPI STDAPI
#define SHDOCAPI_(type) STDAPI_(type)
#endif
#endif // SHDOCAPI
#if !defined(_WIN64)
#include <pshpack1.h>
#endif
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */
#pragma region Desktop Family
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
DECLARE_HANDLE(HDROP);
_Success_(return != 0)
SHSTDAPI_(UINT) DragQueryFileA(_In_ HDROP hDrop, _In_ UINT iFile, _Out_writes_opt_(cch) LPSTR lpszFile, _In_ UINT cch);
_Success_(return != 0)
SHSTDAPI_(UINT) DragQueryFileW(_In_ HDROP hDrop, _In_ UINT iFile, _Out_writes_opt_(cch) LPWSTR lpszFile, _In_ UINT cch);
#ifdef UNICODE
#define DragQueryFile DragQueryFileW
#else
#define DragQueryFile DragQueryFileA
#endif // !UNICODE
SHSTDAPI_(BOOL) DragQueryPoint(_In_ HDROP hDrop, _Out_ POINT *ppt);
SHSTDAPI_(void) DragFinish(_In_ HDROP hDrop);
SHSTDAPI_(void) DragAcceptFiles(_In_ HWND hWnd, _In_ BOOL fAccept);
SHSTDAPI_(HINSTANCE) ShellExecuteA(_In_opt_ HWND hwnd, _In_opt_ LPCSTR lpOperation, _In_ LPCSTR lpFile, _In_opt_ LPCSTR lpParameters,
_In_opt_ LPCSTR lpDirectory, _In_ INT nShowCmd);
SHSTDAPI_(HINSTANCE) ShellExecuteW(_In_opt_ HWND hwnd, _In_opt_ LPCWSTR lpOperation, _In_ LPCWSTR lpFile, _In_opt_ LPCWSTR lpParameters,
_In_opt_ LPCWSTR lpDirectory, _In_ INT nShowCmd);
#ifdef UNICODE
#define ShellExecute ShellExecuteW
#else
#define ShellExecute ShellExecuteA
#endif // !UNICODE
_Success_(return > 32) // SE_ERR_DLLNOTFOUND
SHSTDAPI_(HINSTANCE) FindExecutableA(_In_ LPCSTR lpFile, _In_opt_ LPCSTR lpDirectory, _Out_writes_(MAX_PATH) LPSTR lpResult);
_Success_(return > 32) // SE_ERR_DLLNOTFOUND
SHSTDAPI_(HINSTANCE) FindExecutableW(_In_ LPCWSTR lpFile, _In_opt_ LPCWSTR lpDirectory, _Out_writes_(MAX_PATH) LPWSTR lpResult);
#ifdef UNICODE
#define FindExecutable FindExecutableW
#else
#define FindExecutable FindExecutableA
And that tells me the error Microsoft Visual Studio 2013 is:
Warning 1 warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification File:win32_test_core.obj Project:test_ndef_url
Error 2 error LNK2019: unresolved external symbol __imp__ShellExecuteW#24 referenced in function _ReadMessageOnAnyTagCompletedURL File:win32_test_ndef_url.obj Project:test_ndef_url
Error 3 error LNK1120: 1 unresolved externals File:test_ndef_url.exe Project:test_ndef_url
Please, I need help with this.
You are missing the actual body (or a placeholder for a function body) of the ShellExecute function (OK, ShellExecuteW, but that's a detail you don't need to worry about now.) What's in the header is the declaration, but you still need the definition. In this case, that's in a library.
Do these:
Google for "MSDN ShellExecute". MSDN is Microsoft Developer Network, and the place to go for Windows API documentation.
The first link is to https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx (for now; Microsoft's websites have been awful at keeping links stable for future reference.)
That's the documentation of the function. Scroll down to the bottom of the page. There is a table that lists the "requirements" for this function. One of them is the "Library", which is "Shell32.lib" in this case.
You need to link to this library. The easiest way, in Visual C++ (which you are using) and for a standard Windows library (which this is) is to add the following line to one of your source files (maybe at the very top): #pragma comment (lib, "Shell32"). (Note the lack of a semicolon.)
and that's it. There are better (more scalable, flexible and portable) methods for larger projects, but you should be fine for this small sample.

Intellisense: argument of type "HWND" is incompatible with parameter of type "rawData::HWND"

I have been improving a class that I have been working on and one of those things is creating libraries to handle certain tasks that are not really needed to be seen in my main project. However when I call the class and instantiate the library I just created I get:
Intellisense: argument of type "HWND" is incompatible with parameter
of type "rawData::HWND"
This is happening when I call a method from the library and pass these arguments ( the error is in the first argument):
// register the window for touch instead of gestures
rawData::RegisterTouchWindow(hWnd, 0);
The complete code for this here:
//Creates the main window
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
hInst = hInstance; // Store instance handle in our global variable
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd) {
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
// register the window for touch instead of gestures
/*ERROR HERE*/rawTData::RegisterTouchWindow(hWnd, 0);/*ERROR HERE*/
// the following code initializes the points
for (int i=0; i< MAXPOINTS; i++){
points[i][0] = -1;
points[i][1] = -1;
idLookup[i] = -1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
I have gone to a few websites and they suggest using "this->" to specify which variable you are pointing to but nothing shows when I try it. It's why I came here to see if anyone has ran into this situation. Any little help would b most appreciated. Thanks in advance!
Also, here is the source of my library class:
#ifndef RAWTOUCHDATA_H
#define RAWTOUCHDATA_H
#pragma once
namespace rawTData
{
//#define __in __allowed(on_parameter)
#if (defined(_M_IX86) || defined(_M_IA64) || defined(_M_AMD64)) && !defined(MIDL_PASS)
#define DECLSPEC_IMPORT __declspec(dllimport)
#else
#define DECLSPEC_IMPORT
#endif
//
// Define API decoration for direct importing of DLL references.
//
#if !defined(_USER32_)
#define WINUSERAPI DECLSPEC_IMPORT
#define WINABLEAPI DECLSPEC_IMPORT
#else
#define WINUSERAPI
#define WINABLEAPI
#endif
#ifndef NO_STRICT
#ifndef STRICT
#define STRICT 1
#endif
#endif /* NO_STRICT */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifndef WINVER // Specifies that the minimum required platform is Windows 7.
#define WINVER 0x0601 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows 7.
#define _WIN32_WINNT 0x0601 // Change this to the appropriate value to target other versions of Windows.
#endif
//
// The following types are guaranteed to be signed and 32 bits wide.
//
typedef signed int LONG32, *PLONG32;
//
// The following types are guaranteed to be unsigned and 32 bits wide.
//
typedef unsigned int ULONG32, *PULONG32;
typedef unsigned int DWORD32, *PDWORD32;
#if !defined(_W64)
#if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
#define _W64 __w64
#else
#define _W64
#endif
#endif
//
// The INT_PTR is guaranteed to be the same size as a pointer. Its
// size with change with pointer size (32/64). It should be used
// anywhere that a pointer is cast to an integer type. UINT_PTR is
// the unsigned variation.
//
// __int3264 is intrinsic to 64b MIDL but not to old MIDL or to C compiler.
//
#if ( 501 < __midl )
typedef [public] __int3264 INT_PTR, *PINT_PTR;
typedef [public] unsigned __int3264 UINT_PTR, *PUINT_PTR;
typedef [public] __int3264 LONG_PTR, *PLONG_PTR;
typedef [public] unsigned __int3264 ULONG_PTR, *PULONG_PTR;
#else // midl64
// old midl and C++ compiler
#if defined(_WIN64)
typedef __int64 INT_PTR, *PINT_PTR;
typedef unsigned __int64 UINT_PTR, *PUINT_PTR;
typedef __int64 LONG_PTR, *PLONG_PTR;
typedef unsigned __int64 ULONG_PTR, *PULONG_PTR;
#define __int3264 __int64
#else
typedef _W64 int INT_PTR, *PINT_PTR;
typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
typedef _W64 long LONG_PTR, *PLONG_PTR;
typedef _W64 unsigned long ULONG_PTR, *PULONG_PTR;
#define __int3264 __int32
#endif
#endif // midl64
//
// Define API decoration for direct importing system DLL references.
//
#if !defined(_NTSYSTEM_)
#define NTSYSAPI DECLSPEC_IMPORT
#define NTSYSCALLAPI DECLSPEC_IMPORT
#else
#define NTSYSAPI
#if defined(_NTDLLBUILD_)
#define NTSYSCALLAPI
#else
#define NTSYSCALLAPI DECLSPEC_ADDRSAFE
#endif
#endif
//
// Basics
//
#ifndef VOID
#define VOID void
typedef char CHAR;
typedef short SHORT;
typedef long LONG;
#if !defined(MIDL_PASS)
typedef int INT;
#endif
#endif
#ifndef WINVER
#define WINVER 0x0500
#endif /* WINVER */
/*
* BASETYPES is defined in ntdef.h if these types are already defined
*/
#ifndef BASETYPES
#define BASETYPES
typedef unsigned long ULONG;
typedef ULONG *PULONG;
typedef unsigned short USHORT;
typedef USHORT *PUSHORT;
typedef unsigned char UCHAR;
typedef UCHAR *PUCHAR;
typedef char *PSZ;
#endif /* !BASETYPES */
#define MAX_PATH 260
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef IN
#define IN
#endif
#ifndef OUT
#define OUT
#endif
#ifndef OPTIONAL
#define OPTIONAL
#endif
#undef far
#undef near
#undef pascal
#define far
#define near
#if (!defined(_MAC)) && ((_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED))
#define pascal __stdcall
#else
#define pascal
#endif
#if defined(DOSWIN32) || defined(_MAC)
#define cdecl _cdecl
#ifndef CDECL
#define CDECL _cdecl
#endif
#else
#define cdecl
#ifndef CDECL
#define CDECL
#endif
#endif
#ifdef _MAC
#define CALLBACK PASCAL
#define WINAPI CDECL
#define WINAPIV CDECL
#define APIENTRY WINAPI
#define APIPRIVATE CDECL
#ifdef _68K_
#define PASCAL __pascal
#else
#define PASCAL
#endif
#elif (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)
#define CALLBACK __stdcall
#define WINAPI __stdcall
#define WINAPIV __cdecl
#define APIENTRY WINAPI
#define APIPRIVATE __stdcall
#define PASCAL __stdcall
#else
#define CALLBACK
#define WINAPI
#define WINAPIV
#define APIENTRY WINAPI
#define APIPRIVATE
#define PASCAL pascal
#endif
#ifdef _M_CEE_PURE
#define WINAPI_INLINE __clrcall
#else
#define WINAPI_INLINE WINAPI
#endif
#undef FAR
#undef NEAR
#define FAR far
#define NEAR near
#ifndef CONST
#define CONST const
#endif
typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef float FLOAT;
typedef FLOAT *PFLOAT;
typedef BYTE near *PBYTE;
typedef BYTE far *LPBYTE;
typedef int near *PINT;
typedef int far *LPINT;
typedef WORD near *PWORD;
typedef WORD far *LPWORD;
typedef long far *LPLONG;
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;
typedef void far *LPVOID;
typedef CONST void far *LPCVOID;
typedef int INT;
typedef unsigned int UINT;
typedef unsigned int *PUINT;
#if(WINVER >= 0x0601)
#define WM_TOUCH 0x0240
#endif /* WINVER >= 0x0601 */
//
// Handle to an Object
//
#ifdef STRICT
typedef void *HANDLE;
#if 0 && (_MSC_VER > 1000)
#define DECLARE_HANDLE(name) struct name##__; typedef struct name##__ *name
#else
#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
#endif
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif
typedef HANDLE *PHANDLE;
#ifndef WIN_INTERNAL
DECLARE_HANDLE (HWND);
DECLARE_HANDLE (HHOOK);
#ifdef WINABLE
DECLARE_HANDLE (HEVENT);
#endif
#endif
typedef const RECTL FAR* LPCRECTL;
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT;
typedef struct _POINTL /* ptl */
{
LONG x;
LONG y;
} POINTL, *PPOINTL;
typedef struct tagSIZE
{
LONG cx;
LONG cy;
} SIZE, *PSIZE, *LPSIZE;
typedef SIZE SIZEL;
typedef SIZE *PSIZEL, *LPSIZEL;
typedef struct tagPOINTS
{
#ifndef _MAC
SHORT x;
SHORT y;
#else
SHORT y;
SHORT x;
#endif
} POINTS, *PPOINTS, *LPPOINTS;
#if(WINVER >= 0x0601)
/*
* Touch Input defines and functions
*/
/*
* Touch input handle
*/
DECLARE_HANDLE(HTOUCHINPUT);
typedef struct tagTOUCHINPUT {
LONG x;
LONG y;
HANDLE hSource;
DWORD dwID;
DWORD dwFlags;
DWORD dwMask;
DWORD dwTime;
ULONG_PTR dwExtraInfo;
DWORD cxContact;
DWORD cyContact;
} TOUCHINPUT, *PTOUCHINPUT;
typedef TOUCHINPUT const * PCTOUCHINPUT;
/*
* Conversion of touch input coordinates to pixels
*/
#define TOUCH_COORD_TO_PIXEL(l) ((l) / 100)
/*
* Touch input flag values (TOUCHINPUT.dwFlags)
*/
#define TOUCHEVENTF_MOVE 0x0001
#define TOUCHEVENTF_DOWN 0x0002
#define TOUCHEVENTF_UP 0x0004
#define TOUCHEVENTF_INRANGE 0x0008
#define TOUCHEVENTF_PRIMARY 0x0010
#define TOUCHEVENTF_NOCOALESCE 0x0020
#define TOUCHEVENTF_PEN 0x0040
#define TOUCHEVENTF_PALM 0x0080
/*
* Touch input mask values (TOUCHINPUT.dwMask)
*/
#define TOUCHINPUTMASKF_TIMEFROMSYSTEM 0x0001 // the dwTime field contains a system generated value
#define TOUCHINPUTMASKF_EXTRAINFO 0x0002 // the dwExtraInfo field is valid
#define TOUCHINPUTMASKF_CONTACTAREA 0x0004 // the cxContact and cyContact fields are valid
WINUSERAPI
BOOL
WINAPI
ScreenToClient(
HWND hWnd,
LPPOINT lpPoint);
WINUSERAPI
BOOL
WINAPI
GetTouchInputInfo(
HTOUCHINPUT hTouchInput, // input event handle; from touch message lParam
UINT cInputs, // number of elements in the array
PTOUCHINPUT pInputs, // array of touch inputs
int cbSize); // sizeof(TOUCHINPUT)
WINUSERAPI
BOOL
WINAPI
CloseTouchInputHandle(
HTOUCHINPUT hTouchInput); // input event handle; from touch message lParam
/*
* RegisterTouchWindow flag values
*/
#define TWF_FINETOUCH (0x00000001)
#define TWF_WANTPALM (0x00000002)
WINUSERAPI
BOOL
WINAPI
RegisterTouchWindow(
HWND hwnd,
ULONG ulFlags);
WINUSERAPI
BOOL
WINAPI
UnregisterTouchWindow(
HWND hwnd);
WINUSERAPI
BOOL
WINAPI
IsTouchWindow(
HWND hwnd,
PULONG pulFlags);
#endif /* WINVER >= 0x0601 */
#ifdef __cplusplus
}
#endif /* __cplusplus */
}
#endif //RAWTOUCHDATA_H
By putting everything in your own namespace rawTData, you've declared all of those types to belong to rawTData. HWND is a Windows API type, and is not equivalent to your definition (which is rawTData::HWND) Namespaces are not simply for wrapping a bunch of code you don't want Intellisense to show, they "hide" names in a directory-like structure. A simple fix is to move the namespace rawTData { down to above your own declarations but it begs the question: why are you wrapping everything in namespace rawTData?
It looks like you've cut and pasted a bunch of Windows.h definitions into your own namespace. You really should just include the appropriate file. If that doesn't work there are likely other problems that need solving, and you shouldn't cut around it by copy-pasting like this.

Return double* from c++ dll to delphi program

Im trying to return the array of double (declared as double*) to delphi program. In c++ dll project I have
#define DllExport __declspec( dllexport )
extern double* array;
extern "C"
{
DllExport double* SomeMethod(double);
}
and array got deleted when Dll is unloaded
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch( ul_reason_for_call )
{
...
case DLL_PROCESS_DETACH:
delete [] array;
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
While I was testing my dll in c++ console application I got the right result after using SomeMethod from dll. Next i tried to test my dll in Delphi but the content of array returned by method is wrong. I used the following code.
TSomeMethod = function(level : Double): PDouble; cdecl;
...
var
SomeMethod: TSomeMethod;
arr: PDouble;
...
if Assigned(SomeMethod) then
begin
arr:= SomeMethod(15);
writeln(arr^:2:0);
inc(arr);
writeln(arr^:2:0);
end
...
What is a proper way to return a double* from c++ dll to use in delphi?
P.S. Other methods work in th right way. For example dll returns char* and in delphi I got it using PAnsiChar
UPDATE
Here is some C++ code from file where SomeMethod is written.
double* array; // yea it's an array that declared as external in other file;
...
double* SomeMethod(double level)
{
...
deque<double> arrayToReturn;
... // some actions with deque
array= new double[arrayToReturn.size()];
for (unsigned int i = 0; i<arrayToReturn.size(); i++)
array[i] = arrayToReturn[i];
return array;
}
The code in your question works fine. Which means that the problem lies elsewhere. Here's the proof:
C++
#include <Windows.h>
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" {
__declspec(dllexport) double* SomeMethod(double)
{
double* array = new double[2];
array[0] = 42;
array[1] = 666;
return array;
}
}
Delphi
{$APPTYPE CONSOLE}
uses
Windows, SysUtils;
type
TSomeMethod = function(level : Double): PDouble; cdecl;
var
SomeMethod: TSomeMethod;
arr: PDouble;
lib: HMODULE;
begin
lib := LoadLibrary('MyDll.dll');
Win32Check(lib<>0);
SomeMethod := GetProcAddress(lib, 'SomeMethod');
Win32Check(Assigned(SomeMethod));
arr:= SomeMethod(15);
Writeln(arr^:3:0);
inc(arr);
Writeln(arr^:3:0);
Readln;
end.
Output
42
666

Creating Custom Simple Mapi DLL, fails when executing

i need to create small dll to intercept simple mapi calls and send files via:
file->Send to as attachment (in excel, word, acrobat reader...)
or via explorer->rightclickmenu->Send to->Mail recipient
to attach to gmail.
after reading this:
Mapi32.dll Stub Registry Settings (Windows)
Mapi32 Stub Library (Windows)
and searching here on stackoverflow and other webs i have created one very small dll using code::blocks that shows me information for investigating the process.
I have installed it on registry correctly like indicated in previous links on HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\SimpMapi.
I have selected on my win xp pro in control panel->Internet options my SimpMapi client.
when i open excel write some cells and go to File->Send like attachment dll shows me that first calls MapiLogon, second calls MapiSendMail and third when i close excel it calls MapiLogoff, but then it throws an error and closes excel.exe.
If i try it from explorer window right clicking one file and SendTo->Mail recipient it calls directly MapiSendMail and when i click on ok in MsgBox it throws an error and closes explorer.exe
Could you help me to find out what is wrong.
This is the mail.h file:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
// Todo lo necesario a incluir por mapi.h
#define SUCCESS_SUCCESS 0
#define MAPI_E_USER_ABORT 1
#define MAPI_E_LOGIN_FAILURE 3
typedef unsigned long FLAGS;
typedef unsigned long LHANDLE;
typedef unsigned long FAR *LPLHANDLE, FAR *LPULONG;
typedef struct {
ULONG ulReserved;
ULONG ulRecipClass;
LPSTR lpszName;
LPSTR lpszAddress;
ULONG ulEIDSize;
LPVOID lpEntryID;
} MapiRecipDesc, *lpMapiRecipDesc;
typedef struct {
ULONG ulReserved;
ULONG flFlags;
ULONG nPosition;
LPSTR lpszPathName;
LPSTR lpszFileName;
LPVOID lpFileType;
} MapiFileDesc, *lpMapiFileDesc;
typedef struct {
ULONG ulReserved;
LPSTR lpszSubject;
LPSTR lpszNoteText;
LPSTR lpszMessageType;
LPSTR lpszDateReceived;
LPSTR lpszConversationID;
FLAGS flFlags;
lpMapiRecipDesc lpOriginator;
ULONG nRecipCount;
lpMapiRecipDesc lpRecips;
ULONG nFileCount;
lpMapiFileDesc lpFiles;
} MapiMessage, *lpMapiMessage;
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
extern "C" void DLL_EXPORT SomeFunction(const LPCSTR sometext);
extern "C" ULONG DLL_EXPORT MAPILogon(ULONG_PTR ulUIParam,LPSTR lpszProfileName,LPSTR lpszPassword,FLAGS flFlags,ULONG ulReserved,LPLHANDLE lplhSession);
extern "C" ULONG DLL_EXPORT MAPILogoff(LHANDLE lhSession,ULONG_PTR ulUIParam,FLAGS flFlags,ULONG ulReserved);
extern "C" ULONG DLL_EXPORT MAPISendDocuments(ULONG_PTR ulUIParam,LPSTR lpszDelimChar,LPSTR lpszFilePaths,LPSTR lpszFileNames,ULONG ulReserved);
extern "C" ULONG DLL_EXPORT MAPISendMail(LHANDLE lhSession,ULONG_PTR ulUIParam,lpMapiMessage lpMessage,FLAGS flFlags,ULONG ulReserved);
#endif
This is the main.cpp file:
#include "main.h"
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
ULONG DLL_EXPORT MAPILogon(ULONG_PTR ulUIParam,LPSTR lpszProfileName,LPSTR lpszPassword,FLAGS flFlags,ULONG ulReserved,LPLHANDLE lplhSession)
{
MessageBoxA(0, "MAPILogon", "MAPILogon", MB_OK | MB_ICONINFORMATION);
return SUCCESS_SUCCESS;
}
ULONG DLL_EXPORT MAPILogoff(LHANDLE lhSession,ULONG_PTR ulUIParam,FLAGS flFlags,ULONG ulReserved)
{
MessageBoxA(0, "MAPILogoff", "MAPILogoff", MB_OK | MB_ICONINFORMATION);
return SUCCESS_SUCCESS;
}
ULONG DLL_EXPORT MAPISendDocuments(ULONG_PTR ulUIParam,LPSTR lpszDelimChar,LPSTR lpszFilePaths,LPSTR lpszFileNames,ULONG ulReserved)
{
MessageBoxA(0, "MAPISendDocuments", "MAPISendDocuments", MB_OK | MB_ICONINFORMATION);
return SUCCESS_SUCCESS;
}
ULONG DLL_EXPORT MAPISendMail(LHANDLE lhSession,ULONG_PTR ulUIParam,lpMapiMessage lpMessage,FLAGS flFlags,ULONG ulReserved)
{
MessageBoxA(0, "MAPISendMail", "MAPISendMail", MB_OK | MB_ICONINFORMATION);
return SUCCESS_SUCCESS;
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
Thanks in advance.
Jorge
In link: Creating Custom Simple Mapi DLL, throws error when executing
Andy helped me to solve the problem that was incorrect calling convention.
Adding WINAPI to all my Simple MAPI function declarations and definitions it worked, example:
extern "C" ULONG DLL_EXPORT WINAPI MAPILogon( // etc
Then the problem was that my exported functions were exported with declarations.
To correct this in code::blocks you have to add in:
Main menu: Project -> Build options -> GNU GCC Compiler -> Linker settings -> Other linker options: -Wl,--kill-at
Thanks!