The type of project you create will depend on where and how the DLL will be used.
If you are going to continue to use MANAGED code, there is really no need to convert the C# to C++, just use the DLL and call its functions.
If you are converting this to UNMANAGED C++, there are a lot of other things you need to consider such as how you're going to do that WebService call.
If you JUST need to create a DLL in managed C++ that does a WebService call, there are easier options.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
First:
Where will the exported DLL function be used?
What I'm saying is that if it's already in a DLL, there is no need to rewrite it.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
i have an application need to insert this DLL. ...
Is the application that needs the DLL a managed (dot net) application or a native (NOT dot net) application?
Was it written in C++ or something else?
I'm assuming you have the source code to the application. Is that correct?
I'm asking so many questions because this could be a significant amount of work if it REALLY needs to be converted to a native C++ DLL.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
I understand now.
No, this cannot be done the way you want it to be done.
The error you will get will be:
__declspec(dllimport) cannot be applied to a member of a managed type
Your best bet would be to:
1) rename THIS method callSetup to something like callSetupM and re-compile this DLL withC#
2) create a new DLL in C++ in a class library WITH /clr turned on.
3) create an exported method called callSetup(...,...)
that calls the callSetupM method from the C# DLL
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Yes. I tried an example and it worked.
Of course, I could not use your exact code because it references a web service I don't have referenced.
Here is the C# that references (and makes) a web service call:
using System.Xml;
using CSharpDll.WS_WSX;
namespace CSharpDll
{
public class CCSharpDll
{
public static string GetCity(string strZip)
{
USZip svc = new USZip();
XmlNode node = svc.GetInfoByZIP(strZip);
return (node.LastChild.SelectNodes("CITY").Item(0).InnerText);
}
}
}
Here is the C++ code (Win32 DLL with /clr turned on, (with MFC and MultiByte character set):
#include <afx.h>
#include "stdafx.h"
#include "CPlusPlusToCSharp.h"
using namespace System;
using namespace CSharpDll;
__declspec(dllexport) char* GetCity(char* pstrZipCode) //; use this 1 line in the header
{
String^ strZipM = gcnew String(CString(pstrZipCode));
static CString strRetVal;
strRetVal = CString(CSharpDll::CCSharpDll::GetCity(strZipM));
return strRetVal.GetBuffer(0);
}
Here is the NATIVE ONLY C++ code that references calls the hybrid DLL that calls the managed (C#) DLL:
#include "stdafx.h"
#include "c:\science\DaniWeb\CPlusPlusToCSharp\CPlusPlusToCSharp.h"
int main(void)
{
puts(GetCity("66251"));
return 0;
}
If YOUR native DLL has the function prototype the other software expects, then you can call a hybrid DLL that calls the one compiled in C#.
This is ONLY this complex because it's mixing native AND managed code.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402