Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Definition of dllimport not allowed 1

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
0
0
TH
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

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);
}

 
Hello,

I got it working. However, I am not totally sure why.

Why did it work when I removed the NATIVEDLL_API from the function definitions, and what are the macros for?

Can you explain the code below?

Many thanks for you help,

Steve

Code:
#ifdef NATIVEDLL_EXPORTS
#define NATIVEDLL_API extern "C" __declspec(dllexport)
#else
#define NATIVEDLL_API __declspec(dllimport)
#endif
 
The NativeDLL.h is used both for the building of NativeDLL.dll (from compiling NativeDll.cpp) as well as in the compilation of whatever source code you are calling the NativeDLL exported functions from.

Thus, the definition NATIVEDLL_API means different things in different circumstances.

When you compile NativeDLL, NATIVEDLL_API has the meaning __declspec(dllexport) which tells the compiler/linker that these functions are being exported in the resulting DLL. To ensure this, your project settings for the building of NativeDLL must define NATIVEDLL_EXPORTS, which is the normal convention that MSVC 6 or MS VisStudio will adopt for DLL projects.

When you build your project that uses NativeDLL.dll, you will #include the NativeDLL.h file and without this project having a definition for NATIVEDLL_EXPORTS, the compiler will understand that functions declared in NativeDLL.h will be imported from a DLL, because NATIVEDLL_API has the meaning __declspec(dllimport).
 
Thanks for your help, problem solved.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top