I am using VCEE and am trying to create a dll using /clr, can this be done I get an error...
StationFormat.obj : fatal error LNK1306: DLL entry point "public: class System::String ^ __clrcall StationFormat::stnFrmt::toStnFrmt(double,int)" ( toStnFrmt@stnFrmt@StationFormat@@$$FQAMP$AAVString@System@@NH@Z) cannot be managed; compile to native
Can I compile to native

Compile to Native
NiceGuys
To use a DLL once you have compiled it, you have to add a reference to it in your EXE project. You can do this in one of two ways:
If the DLL is open in the same solution as your exe, then you can go to Project References -> Add New Reference. From the Dialog Box that appears, select or browse to your DLL project and press OK. The exe project should now build successfully.
Alternatively (I assume your DLL is called DLLName.DLL)...
When you made the DLL, it created two extra files DLLName.lib and DLLName.exp. Copy the lib file to the exe directory, then, in Project Properties -> Linker -> Input -> Additional Dependencies, add the name of that lib file to the list (ie. DLLName.lib)
To use .Net classes/methods through DLLs, make sure they are declared public. There's no need to declare such methods as __declspec(dllexport) (.Net methods aren't exposed through DLL exports, they are exposed through the .NET metadata).
chooch
//StationFormat.cpp This is the function for the dll
#include
"StationFormat.h"using
namespace StationFormat;using
namespace System;String^ stnFrmt::toStnFrmt(
double station, int precision){
String^ decimalPrecision =
""; int i; for (i = 0; i < precision; i++){
decimalPrecision = decimalPrecision +
"0";}
return station.ToString("######0+##." + decimalPrecision);}
This is the part which is bugging me. Don't you need to put this in the namespace too. I usually don't see this kind of layout on a managed class because visual studio automatically puts the code logic into the header file, but in this case don't you need to put that method into the namespace too.
//StationFormat.cpp This is the function for the dll
#include
"StationFormat.h" using namespace System;namespace StationFormat
{
String^ stnFrmt::toStnFrmt(double station, int precision)
{
String^ decimalPrecision = "";
int i;
for (i = 0; i < precision; i++)
{
decimalPrecision = decimalPrecision + "0";
}
return station.ToString("######0+##." + decimalPrecision);
}
}
Have it like that, either that or put it into the class decleration instead.
Eric F. Kaufman
Ok, after all your problems I decided to do a small test project with the same situation as you and it works just fine. Here is the code that I have so you know what I did so maybe you can use it to correct your own.
// testlib.h
#pragma
onceusing
namespace System;namespace
testlib { public ref class Class1{
// TODO: Add your methods for this class here. public: int Test(int a);};
}
// This is the main DLL file.
#include
"stdafx.h"#include
"testlib.h"namespace
testlib{
int Class1::Test(int fred){
return 0;}
}
// testexe.cpp : main project file.
#include
"stdafx.h"#using
"..\Debug\Testlib.dll" //import the managed assembly metadatausing
namespace System;using
namespace testlib;int
main(){
Class1^ pc =
gcnew Class1();pc->Test(1);
return 0;}
Hope this helps.
hamletas
Thanks Oshah and crescens2k, but I must be doing something wrong because that is how I tried to do it and it still doesn't work. If you could please help, I can either email you my solution. Here is my email to facilitate this: hillebrennerATcox.net. You can contact me and I will send the soultion. Alternatively, here are my .h and .cpp for the dll and also the .cpp to test the dll. They are all in the same solution but are different projects. Once again, thanks for your help, because either I have it all wrong or I have one setting messed up somewhere.
//StationFormat.h this is the header for the dll
namespace
StationFormat{
using namespace System; class stnFrmt{
public: static String^ toStnFrmt(double station, int precision);};
}
//StationFormat.cpp This is the function for the dll
#include
"StationFormat.h"using
namespace StationFormat;using
namespace System;String^ stnFrmt::toStnFrmt(
double station, int precision){
String^ decimalPrecision =
""; int i; for (i = 0; i < precision; i++){
decimalPrecision = decimalPrecision +
"0";}
return station.ToString("######0+##." + decimalPrecision);}
// myDllTest.cpp : main project file. I'm testing the dll with this
#include
"StationFormat.h"using
namespace System;using
namespace StationFormat;int
main(){
double a = 10000.0; int precision = 4;String^ myStation = stnFrmt::toStnFrmt(a, precision);
Console::WriteLine(myStation);
return 0;}
Desdacato
Thanks, crescens2k, that did it, I had several mistakes: I waa tying to call the function rather that declaring with gcnew and all that. I'm still not sure what all my errors were, but I appreciate it.
You, too, Osha.
Final Code as follows...
//StationFormat.h
namespace
StationFormat {using namespace System;
public ref class stnFrmt {
public:
String^ toStnFrmt(double station, int precision);
};
}
//StationFormat.cpp
#include
"StationFormat.h"using
namespace StationFormat;using
namespace System;String^ stnFrmt::toStnFrmt(
double station, int precision){
String^ decimalPrecision =
""; int i; for (i = 0; i < precision; i++){
decimalPrecision = decimalPrecision +
"0";}
return station.ToString("######0+##." + decimalPrecision);}
// myDllTest.cpp : main project file.
//#include "StationFormat.h"
using
namespace System;using
namespace StationFormat;int
main(){
double a = 10000.0; int precision = 4;stnFrmt^ MyStation =
gcnew stnFrmt();Console::WriteLine(MyStation->toStnFrmt(a,precision));
return 0;}
Jamie_M_
Here is my CPP file, it is Managed code, I think. If I don't compile with /clr I get all kind of errors. Is it a fact that I just can't declare a function as a "String^" and return the "String^" value Any help is greatly appreciated. When I take away the entry point it compiles fine, but then when I try to use the DLL I get an error...
myDllTest.obj : error LNK2001: unresolved external symbol "public: static class System::String ^ __clrcall StationFormat::stnFrmt::toStnFrmt(double,int)" ( toStnFrmt@stnFrmt@StationFormat@@$$FSMP$AAVString@System@@NH@Z)
//StationFormat.cpp
#include
"StationFormat.h"using
namespace StationFormat;using
namespace System;String^ stnFrmt::toStnFrmt(
double station, int precision){
String^ decimalPrecision =
""; int i; for (i = 0; i < precision; i++){
decimalPrecision = decimalPrecision +
"0";}
return station.ToString("######0+##." + decimalPrecision);}
DOCMAN
Thanks, crescens2k, but I still get the following same two errors:
myDllTest.obj : error LNK2001: unresolved external symbol "public: static class System::String ^ __clrcall StationFormat::stnFrmt::toStnFrmt(double,int)" ( toStnFrmt@stnFrmt@StationFormat@@$$FSMP$AAVString@System@@NH@Z)
C:\Documents and Settings\Brent Hillebrenner\My Documents\Visual Studio 2005\Projects\HowDoI\MyDll\Release\dllTest.exe : fatal error LNK1120: 1 unresolved externals
The entire project compiles as follows:
------ Rebuild All started: Project: MyDll, Configuration: Release Win32 ------
Deleting intermediate and output files for project 'MyDll', configuration 'Release|Win32'
Compiling...
StationFormat.cpp
Linking...
Generating code
Finished generating code
Embedding manifest...
Build log was saved at "file://c:\Documents and Settings\Brent Hillebrenner\My Documents\Visual Studio 2005\Projects\HowDoI\MyDll\MyDll\Release\BuildLog.htm"
MyDll - 0 error(s), 0 warning(s)
------ Rebuild All started: Project: dllTest, Configuration: Release Win32 ------
Deleting intermediate and output files for project 'dllTest', configuration 'Release|Win32'
Compiling...
myDllTest.cpp
Linking...
myDllTest.obj : error LNK2001: unresolved external symbol "public: static class System::String ^ __clrcall StationFormat::stnFrmt::toStnFrmt(double,int)" ( toStnFrmt@stnFrmt@StationFormat@@$$FSMP$AAVString@System@@NH@Z)
C:\Documents and Settings\Brent Hillebrenner\My Documents\Visual Studio 2005\Projects\HowDoI\MyDll\Release\dllTest.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Brent Hillebrenner\My Documents\Visual Studio 2005\Projects\HowDoI\MyDll\dllTest\Release\BuildLog.htm"
dllTest - 2 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
Mac Jamb
You can create both managed and unmanaged dlls, so the problem doesn't lie there. For you the problem is somewhere else and to work that out a code sample is needed.
Under the linker error messages though it seems that you have given the dll an entry point and in 2005 this is not allowed. The following is the information about it in the documentation, hope it helps.
Error Message
DLL entry point function cannot be managed; compile to nativeBeginning in Visual C++ 2005, DllMain cannot be compiled to MSIL; it must be compiled to native.
To resolve,
Compile the file that contains the entry point without /clr.
Put the entry point in a #pragma unmanaged section.
For more information, see
/clr (Common Language Runtime Compilation)
< XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" />managed, unmanaged
Example
The following sample generates LNK1306.
// LNK1306.cpp // compile with: /clr /link /dll /entry:NewDllMain // LNK1306 error expected #include <windows.h> int __stdcall NewDllMain( HINSTANCE h, ULONG ulReason, PVOID pvReserved ) { return 1; }