webservice ASP.net (c++) using unmanaged c++: Link problem

  • Thread starter Kristof Thys via .NET 247
  • Start date
K

Kristof Thys via .NET 247

Post a new message to microsoft.public.dotnet.languages.vc
http://www.dotnet247.com/247reference/default.aspx




Hello,

I've been struggling for weeks with this problem, I hope I find some help here...

To start, in our company, we have a large existing c++ project (native code, unmanaged c++)

The objective is to write a webservice for some of the functionalities from this project.
The best way to do this in Visual Studio .NET 2003 seems to me by creating an ASP.NET Web service in c++.
I create the default template, wich works just fine.
Now I want to use some unmanaged c++ code, so I write a managed wrapperclass and delegate the old routines from identical methods. When I test this with an unmanaged c++ class in the same project, this works fine.
But when I include an header from the old project, and call some old routines, I get link problems for every implemented routine.

e.g.:
error LNK2001: unresolved external symbol "public: __thiscall test::~test(void)" (??1test@@$$FQAE@XZ)
error LNK2001: unresolved external symbol "public: char * __thiscall test::function(int,int)" (?function@test@@$$FQAEPADHH@Z)
...

I've read http://msdn.microsoft.com/library/d...stechart/html/vcconMixedDLLLoadingProblem.asp concerning mixed dll problems, and figured out this could be my problem, and tried the solution at :
http://support.microsoft.com/?id=814472
But here rises another problem for me, none of the given solutions seems applicable to my particular problem,

I don't think my DLL isn't entered using DLL exports (__declspec(dllexport)),
not it seems to be a COM-based DLL.
and "Consumers of your DLL can use managed code, and your DLL contains either DLL exports or managed entry points." doesnt work for my project neither...

So I'm pretty stuck...

I need some help, if you wish you can simulate my problem:

just create a default ASP.NET webservice : called e.g. Webservice

add managedwrapper.h :

#pragma once

#include "test.h"

#pragma managed

__gc class ManagedWrapperClass{
private:
test * t;
public:
ManagedWrapperClass() {t = new test;}
~ManagedWrapperClass() {delete t;}
const char* function() {return t->function();}
};

in webserviceclass.cpp, include "managedwrapper.h" and rewrite the helloworld routine :
String* webserviceClass::HelloWorld()
{
ManagedWrapperClass *m = new ManagedWrapperClass();
return m->function();
}


and now create a console application (.NET) (c++) e.g. testconsole

add a file test.h :

class test{
public:
test() {}
~test() {}
char* function() {return "Wow you did it...";}
};


in webservice -> project settings -> c/c++ -> General -> Additional include Directories : add ../testconsole



I want to thank everyone who tries to help me with this one!
I've just started working and after 3 years of studying in VC6.0.. .NET and webservices are pretty new...



Thanks in Advance,




Kristof Thys
 
K

Kristof

On Mon, 20 Sep 2004 00:34:26 -0700, Kristof Thys via .NET 247


I managed to resolve a part of the above problem, by including msvcrt.lib
to the webservice, (properties->linker->input->additional dependencies)
but the same problem arises when I create a test.cpp in the testconsole,
where I place the body of the function:

test.cpp :

#include "test.h"
char* test::function()
{
return "Wow you really did it...";
}

webservice error LNK2001: unresolved external symbol "public: char *
__thiscall test::function(void)" (?function@test@@$$FQAEPADXZ)

(just as I need to do with the original project...)
 
K

Kristof

I dit the test myself and you are right :S

But when I try to put a test.cpp in the testconsole, with the body of my
function :

////test.cpp

#include "stdafx.h"
#include "test.h"


char* test::function()
{
return "Wow you did it...";
}

I get the same errors I got all week...

thx for the effort!
 
B

Ben Schwehn

Kristof said:
But when I try to put a test.cpp in the testconsole, with the body of
my function : [...]
I get the same errors I got all week...

well, that's not surprising. in the webservice, you're including the
class definition (in test.h) but don't provide a class implementation.

this has nothing to do with mixing managed/unmanaged code.
Same as doing:

#include "stdafx.h"

class NoBody {
public:
static void Foo();
};
int _tmain(int argc, _TCHAR* argv[])
{
NoBody::Foo();
}

-> LNK2001
 
K

Kristof

That's true,


when I include test.cpp, this simple program works fine.

But this way I keep encountering this link problems:

Suppose in test.cpp I change the function:

#include "testclass2.h"

char* function()
{
testclass2 t = new testclass2;
return t->function2();
}

In the testconsole project, this works perfectly.

But when I call test->function from the webservice, the compiler says
testclass2::function2 is unresolved.

Why isn't it unresolved when I build testconsole?

What is the solution to this problem? It can't be the issue to include all
cpp files (testclass2.cpp in this case), that way,
the list of includes would be giant and I will get lots of errors this
way...


Thx for the help!

Kristof said:
But when I try to put a test.cpp in the testconsole, with the body of
my function : [...]
I get the same errors I got all week...

well, that's not surprising. in the webservice, you're including the
class definition (in test.h) but don't provide a class implementation.

this has nothing to do with mixing managed/unmanaged code.
Same as doing:

#include "stdafx.h"

class NoBody {
public:
static void Foo();
};
int _tmain(int argc, _TCHAR* argv[])
{
NoBody::Foo();
}

-> LNK2001
 
B

Ben Schwehn

But this way I keep encountering this link problems:
Suppose in test.cpp I change the function:

#include "testclass2.h"

char* function()
{
testclass2 t = new testclass2;
return t->function2();
}

In the testconsole project, this works perfectly.

But when I call test->function from the webservice, the compiler says
testclass2::function2 is unresolved.

Why isn't it unresolved when I build testconsole?

because it's a different project and probably you have testerclass2.cpp
included in this project -> class testerclass2 gets compiled and linked.
in the webservice you only have the class definition (by including the
header) but not the class implementation.

What is the solution to this problem? It can't be the issue to include
all cpp files (testclass2.cpp in this case), that way,
the list of includes would be giant and I will get lots of errors this
way...

if you want to use classes that are defined in other projects/dlls/libs
you'll have to use any of the standard ways of doing this. eg create a
lib file and staticlly link to it, statically link to an dll or
dynamicly load a dll at runtime, use com, the .net stuff, whatever.

You'll find plenty of info on that via a websearch.

This is really what you're (perhaps unknowingly) doing all the time
already: For example, say you want to use the windows socket function
"WSASend": what you do is
1. include the appropiate header (Winsock2.h) and
2. link to the appropiate library (Ws2_32.lib)
If you don't do 1., you'll get compile time errors (function is not
declared), if you don't do 2. you'll get link time errors (function not
implemented)

Hope this makes things a bit clearer.
 
K

Kristof Thys

Ben said:
because it's a different project and probably you have testerclass2.cpp
included in this project -> class testerclass2 gets compiled and linked.
in the webservice you only have the class definition (by including the
header) but not the class implementation.




if you want to use classes that are defined in other projects/dlls/libs
you'll have to use any of the standard ways of doing this. eg create a
lib file and staticlly link to it, statically link to an dll or
dynamicly load a dll at runtime, use com, the .net stuff, whatever.

You'll find plenty of info on that via a websearch.

This is really what you're (perhaps unknowingly) doing all the time
already: For example, say you want to use the windows socket function
"WSASend": what you do is
1. include the appropiate header (Winsock2.h) and
2. link to the appropiate library (Ws2_32.lib)
If you don't do 1., you'll get compile time errors (function is not
declared), if you don't do 2. you'll get link time errors (function not
implemented)

Hope this makes things a bit clearer.
Thanks very much,
this does clear things up! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top