writing a Class Library....

J

Jim Krikris

Is it anyway possible that i write a Class Library in C# that i can call
from a native win32 c++ project? Not managed C++.
I tried implib on a class library(.lib from .dll) and unfortunately it
seems like there is nothing exported at all (cant really remember the
command atm).
 
A

Arne Vajhøj

Jim said:
Is it anyway possible that i write a Class Library in C# that i can call
from a native win32 c++ project? Not managed C++.
I tried implib on a class library(.lib from .dll) and unfortunately it
seems like there is nothing exported at all (cant really remember the
command atm).

Make the .NET class a COM class and it should be possible to
call it from unmanaged C++.

I would avoid it if possible.

But if are a shark to COM in C++, then your preferences may
be different.

Arne
 
T

Tim Roberts

Jim Krikris said:
Is it anyway possible that i write a Class Library in C# that i can call
from a native win32 c++ project? Not managed C++.
I tried implib on a class library(.lib from .dll) and unfortunately it
seems like there is nothing exported at all ...

There are a couple of ways. You can create COM objects from C#, and call
them using the standard COM mechanisms from anywhere. Or, you can include
a managed C++ wrapper around your C# class and use it to create DLL
exports.
 
J

Jim Krikris

Tim said:
There are a couple of ways. You can create COM objects from C#, and call
them using the standard COM mechanisms from anywhere. Or, you can include
a managed C++ wrapper around your C# class and use it to create DLL
exports.

Do you have an example of the second way?
Lets say how should i edit the following code...


using System;
using System.IO;

namespace MyExports
{
public class DrivesFound
{
public static string[] getDrivesFound() // exported
{
DriveInfo[] Drives = DriveInfo.GetDrives();
String[] ret = new String[Drives.Length];
for(int i=0;i<Drives.Length;i++)
ret = Drives.Name;
return ret;
}
}
}

I dont know if it is even possible to export a struct that would hold
the results.
 
G

Giovanni Dicanio

Do you have an example of the second way?

I created a simple test solution to implement what I would think would be an
example of this "second way" proposed by Tim.
You can find it here (built using VS2008):

http://www.geocities.com/giovanni.dicanio/vc/TestInterop1.zip

In this sample solution, a DLL with a pure C native interface is built. This
DLL uses C++/CLI to call the managed C# class you proposed, and exports the
features of this class to the native world.
This DLL is lik a "bridge" between managed .NET platform and native
platform.

Then, a sample C++ native executable calls this DLL.
Lets say how should i edit the following code...
[...]
I dont know if it is even possible to export a struct that would hold
the results.

In the above solution, I just used a single string as output parameter, in
which the found drives were written, separated using ";" character.

This is some part of the C++/CLI code (this listing is the source code of
the C-interface function exported by the "bridging" DLL):

<code>

extern "C" NATIVEDRIVEENUM_API int __stdcall FindDrives(wchar_t *
listOfDrives, int maxChars)
{
//
// Check parameters
//
ATLASSERT(listOfDrives != NULL);
ATLASSERT(maxChars > 0);

//
// Call the C# class
//

DriveFinder^ driveFinder = gcnew DriveFinder();
array<String ^>^ drives = driveFinder->GetDrivesFound();
int count = drives->Length;

listOfDrives[0] = 0;
if (count == 0)
{
// Nothing found
return 0;
}

//
// Write output parameters
//

CString result;
result += CString(drives[0]);
for (int i = 1; i < count; i++)
{
result += L";";
result += CString(drives);
}

StringCchCopyW( listOfDrives, maxChars, result );
return count;
}

</code>

HTH,
Giovanni
 

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