Click here to Skip to main content
15,917,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DLL

A.dll

This is A.h
C++
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
#define DLL_EXPORT extern "C" __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllexport)
#endif

DLL_EXPORT void function();
DLL_EXPORT char ** ReturnArr;

This is A.c
C++
void function()
{
char *str = "hello";
char *str1 = "how are you?";
ReturnArr = (char **)malloc(sizeof(char*) * 2);
for(;j<2;j++)
{
ReturnArr[j] = (char *) malloc(256);
if(NULL == ReturnArr[j])
break;
}
strcpy(ReturnArr[0],"str");
strcpy(ReturnArr[1],"str1");
}

Now i have Application.c that would use dll
C++
#include <windows.h>
#include <stdio.h>

typedef int (__cdecl *MYPROC)(LPWSTR);

_declspec(dllimport) char ** ReturnArr;

int main( void )
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
int a = 0;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

// Get a handle to the DLL module.

hinstLib = LoadLibrary(TEXT("A.dll"));

// If the handle is valid, try to get the function address.

if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "function");

// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (L"Message sent to the DLL function\n");
printf("%s",Returnarr[0]);
}

fFreeResult = FreeLibrary(hinstLib);
}

// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");

return 0;

}

##In Visual studio CommonProperties->references:i added A.dll its showing me compiler error
Error	1 error LNK2001: unresolved external symbol "__declspec(dllimport) char ** ReturnArr" (__imp_?ReturnArr@@3PAPADA)"
and
Error 2 fatal error LNK1120: 1 unresolved ##externals


tell me a way how can i actually print ReturnArr as a global variable in my application

thanks
Posted
Updated 27-Oct-13 23:53pm
v2
Comments
Dhawal Arora 28-Oct-13 6:05am    
@nv3 not quiet getting it now i am getting the error "Error 12 error LNK2005: _ReturnArr already defined in Agent.obj
nv3 28-Oct-13 6:39am    
Is Agent.c part of you DLL? Looks to me like it is part of your main application. You will have to decide whether ReturnArr should be defined by the DLL or the main app; you cannot define it in both places. To me it looks like your function "function" want to allocate the space and store the pointer in ReturnArr, so it should be part of your DLL. Then the definition that sits in Agent.c must be removed.
Dhawal Arora 28-Oct-13 6:44am    
well agent.c is removed,Now it is giving me the previous 2 errors only,i defined Returnarr in A.c as NULL,but in Application .c i am Getting the Previous two errors only
Dhawal Arora 28-Oct-13 6:15am    
when i am compiling Application.c its giving me the same error

Error 1 error LNK2001: unresolved external symbol "__declspec(dllimport) char ** ReturnArr" (__imp_?ReturnArr@@3PAPADA)"

Error 2 fatal error LNK1120: 1 unresolved ##externals

i defined Return Arr in A.c,but same result in Application
nv3 28-Oct-13 8:07am    
As Richard wrote in his solution it is important that the declaration and definition of the variable match. In your A.c you should also declare the variable as extern "C". Then the linker will find it.

You have not given a definition for ReturnArr in your A.cpp file, but just declared it. Add a line like:
C++
char** ReturnArr = 0;

to your A.cpp file.
 
Share this answer
 
Comments
CPallini 28-Oct-13 6:00am    
5.
Dhawal Arora 28-Oct-13 6:23am    
i am getting error in Application.c

Error 1 error LNK2001: unresolved external symbol "__declspec(dllimport) char ** ReturnArr" (__imp_?ReturnArr@@3PAPADA)"

Error 2 fatal error LNK1120: 1 unresolved ##externals

I have included char ** ReturnArr = NULL in A.c

but not getting the result.
You have declared ReturnArr in your DLL with extern "C" attributes, but in your application you have just declared it as external, so the compiler has generated a C++ style decorated name. You should create a proper header file for the DLL as detailedin http://msdn.microsoft.com/en-us/library/ms235636(VS.90).aspx[^], and then you can include that in your application, and ensure correct declarations are used throughout.
 
Share this answer
 
Comments
nv3 28-Oct-13 8:05am    
My 5! Important hint.
You need to be careful where to declare or define your variable:

1. The declaration can be repeated everywhere you like. All it does is make the variable accessible from different places (files/DLLs). A declaration is like an entry in a telephone register: you can copy the entry wherever you like - however, an entry doesn't actually create a telephone connection!

2. The definition may only exist once! This means it should be inside a *.c or *.cpp file, not inside a header (which may be included by multiple files). The definition is like the actual telephone connection: it is physically installed in one place.

In your case it means you need to define ReturnArr inside A.c (or another .c file inside the same DLL), and then declare it inside A.h (you already did that) to make it accessible. Since A.h gets included by the other files, there is no need to copy the declaration elsewhere. (but you could)
 
Share this answer
 
Comments
Dhawal Arora 28-Oct-13 6:50am    
@Stefan_Lang in A.c i have defined ReturnArr = NULL;

but i want to access that variable in my Application.c and want to print the reasult inside it which i am in A.c(my dll file),but in Application.c it is giving me error

Error 1 error LNK2001: unresolved external symbol "__declspec(dllimport) char ** ReturnArr" (__imp_?ReturnArr@@3PAPADA)"

Error 2 fatal error LNK1120: 1 unresolved ##externals
Stefan_Lang 28-Oct-13 7:02am    
That is not a definition, that is an initialization! You need to write something like
char** ReturnArr;
somewhere within the global scope (outside any function or class body)

A definition always includes the type!
Dhawal Arora 28-Oct-13 7:08am    
i loaded dll into dependencywalker it is loading the export variable correctly, i am writing char ** ReturnArr; outside the void function(),but unable to use in my application
Stefan_Lang 28-Oct-13 7:30am    
Hmm, this should work. If I have another idea I'll let you know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900