Hello
I have downloaded this code from the internet and I am trying to compile it. Howerver, I keep getting these 2 errors which I cannot seem to solve.
Error C2491 StartNativeProcessing definition of dllimport function not allowed
Error C2491 StartNativeProcessingWithControlProc definition of dllimport function not allowed.
Many thanks for any assistance with this code.
Steve
I have downloaded this code from the internet and I am trying to compile it. Howerver, I keep getting these 2 errors which I cannot seem to solve.
Error C2491 StartNativeProcessing definition of dllimport function not allowed
Error C2491 StartNativeProcessingWithControlProc definition of dllimport function not allowed.
Many thanks for any assistance with this code.
Steve
Code:
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the NATIVEDLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// NATIVEDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef NATIVEDLL_EXPORTS
#define NATIVEDLL_API extern "C" __declspec(dllexport)
#else
#define NATIVEDLL_API __declspec(dllimport)
#endif
// ********************************************************
// Callback function type definition for a function that is
// called when the processing performed by the native DLL
// is complete
//
// Function has no return value & no parameters.
// Example: void MyWorkCompleteFunction()
typedef void (CALLBACK *NATIVEWORKCOMPLETEPROC)();
// Callback function type definition for a function that is
// is called within each iteration of the native processing
// loop. Native processing will continue until the provided
// function returns 0;
//
// Function has BOOL return value (int in C#, Integer in VB.NET)
// Function accepts an LPARAM as a parameter (IntPtr in .NET CF)
// The LPARAM parameter passes application defined data
// back to the application. The LPARAM is initially provided
// to the native DLL on the call to StartNativeProcessingWithControlProc
// function.
typedef BOOL (CALLBACK *NATIVEWORKCONTROLPROC)(LPARAM lParam);
// ********************************************************
// Initiate processing on a background thread. When processing is complete,
// the workCompleteProc callback function is called
NATIVEDLL_API void StartNativeProcessing(NATIVEWORKCOMPLETEPROC workCompleteProc);
// Initiate processing on a background thread. On each iteration of the
// loop, the workControlProc function is called with the lParam passed
// as a parameter. The processing on the background thread continues
// until the workControlProc callback function returns 0. When processing
// is complete, the workCompleteProc callback function is called
NATIVEDLL_API void StartNativeProcessingWithControlProc(
NATIVEWORKCONTROLPROC workControlProc, LPARAM lParam, NATIVEWORKCOMPLETEPROC workCompleteProc);
Code:
// NativeDLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "NativeDLL.h"
#include <windows.h>
#include <commctrl.h>
BOOL APIENTRY DllMain( HANDLE 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;
}
// The reference to the NATIVEWORKCOMPLETEPROC callback function
NATIVEWORKCOMPLETEPROC g_workCompleteProc = NULL;
NATIVEWORKCONTROLPROC g_workControlProc = NULL;
// Sleep time to simulate work
const int workSleepTimeInMilliseconds = 1500;
// Forward declerations
DWORD WINAPI ThreadFunc(void* pvThreadParam);
void DoWork();
// Initiate processing on a background thread. When processing is complete,
// the workCompleteProc callback function is called
NATIVEDLL_API void StartNativeProcessing(NATIVEWORKCOMPLETEPROC workCompleteProc)
{
g_workCompleteProc = workCompleteProc;
g_workControlProc = NULL;
DWORD dwThreadId;
CreateThread(0, 0, ThreadFunc, NULL, 0, &dwThreadId);
}
// Initiate processing on a background thread. On each iteration of the
// loop, the workControlProc function is called with the lParam passed
// as a parameter. The processing on the background thread continues
// until the workControlProc callback function returns 0. When processing
// is complete, the workCompleteProc callback function is called
NATIVEDLL_API void StartNativeProcessingWithControlProc(
NATIVEWORKCONTROLPROC workControlProc, LPARAM lParam, NATIVEWORKCOMPLETEPROC workCompleteProc)
{
g_workCompleteProc = workCompleteProc;
g_workControlProc = workControlProc;
DWORD dwThreadId;
CreateThread(0, 0, ThreadFunc, (void*)lParam, 0, &dwThreadId);
}
// Functioning running on background thread that simulates
// some sort of data processing. If only a NATIVEWORKCOMPLETEPROC
// callback function is provided, ThreadFunc will go through the loop once
// If a NATIVEWORKCONTROLPROC callback function is provided, ThreadFunc will loop
// until the NATIVEWORKCONTROLPROC callback function returns 0
DWORD WINAPI ThreadFunc(void* pvThreadParam)
{
int loopControl = 0;
do
{
DoWork();
if (g_workControlProc != NULL)
loopControl = g_workControlProc((LPARAM)pvThreadParam);
}
while(loopControl != 0);
if (g_workCompleteProc != NULL)
g_workCompleteProc();
return 0;
}
// Simulate doing work for sleeping the specified period of time
void DoWork()
{
Sleep(workSleepTimeInMilliseconds);
}