Hi,I am new with this forum even if I have been programming in c,c++ for a while.
I have never realized any DLL file and I'd like to start now. I think I have basically understood the concepts that stay behind a DLL realization but I still can't make one by my self. I hope someone will give me some hints.
What I have been up to, as far, is the realization of a simple DLL file that should receive a vector of integers and add 1 unit to each of its element.
It seems really simple but there are problems I still can't solve by myself.I am presently working with visual studio 2008 express edition.
I ahve created a project that contains my DLL file, it is composed of .h and .cpp files
Here is my code:

SimpleDLL.h file:

#ifndef INDLL_H
#define INDLL_H
#ifdef EXPORTING_DLL
extern __declspec(dllexport) void vector_modify(int* x);
#endif
#endif

SimpleDLL.cpp file:

#include "SampleDLL.h"
#include <iostream>
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
        )

{
    return TRUE;
}
void vector_modify(int* x)
{
 int i = 0;
 for (i=0; i<3; i++) *(x + i) = i ++;
}

This builds correctly.
The problem is that when I try to build a .cpp file from another project that calls the DLL:

#include "SampleDLL.h"
#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{  
  int i = 0;
  int x[3]={0,0,0};

  vector_modify(x);

  return 0;
}

(In this application I just defined a vector x and I pass it to the vector_modify function).
I obtain this :

 error C3861: 'vector_modify': identifier not found

It seems that the compiler doesn't recognize the function but I have correctly added the SimpleDLL.h file into the project folder and also among the linker options I have added the .lib file.
Moreover I would like to know if is correct the way I pass the vector to the DLL file,printing out the values after the vector_modify() call I should find out something like 1,2,3 (the vector x values incremented of 1 unit)Am I right?

To export symbols from a shared library, you need to use the __declspec(dllexport) attribute as you have done.

A program that uses exported symbols from a DLL needs to import them using __declspec(dllimport).

It is usually a good idea to have a single header file for the shared library, which will declare the public symbols as __declspec(dllexport) when used in the library and as __declspec(dllimport) when used in a program that uses the library.

see: http://msdn.microsoft.com/en-us/library/8fskxacy%28VS.80%29.aspx
and http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c9855

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.