Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Definition of dllimport not allowedHelpful Member! 

robUK2 (Programmer)
10 Mar 08 13:11
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);
}

robUK2 (Programmer)
10 Mar 08 13:46
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
Helpful Member!  noodles1 (Programmer)
25 Mar 08 20:09
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).
robUK2 (Programmer)
26 Mar 08 23:24
Thanks for your help, problem solved.

Steve

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close