P/Invoke in Managed Code

This is a problem that I've been working on all weekend, and I can't figure it out.

For class, we had to create a native DLL which exports template classes of data structures. The DLL compiles, but I'm not sure how to import the classes into managed code.

For our last assignment of the semester, we were told to use P/Invoke and Marshalling. I can't figure out how to P/Invoke a class to be instantiated. Every example I can find for P/Invoke just shows how to import methods from user32.dll or some other Windows DLL.

We followed the KB article here: http://support.microsoft.com/kb/168958

Here is an example of how we've done things:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef NODE_H
#define NODE_H

/* 
Define NULL pointer value 
see: http://en.allexperts.com/q/C-1040/NULL-value-C-C-1.htm (03/09)
*/
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif

// see: http://www.bgsu.edu/departments/compsci/docs/templates.html

namespace INFO450
{
	template<class T>
	class Node
	{
	public:
		// The value of the node
		T value;

		// A pointer to the node to the left
		Node *ptrLeft;

		// A pointer to the node to the right
		Node *ptrRight;

		// A pointer to the parent node
		Node *ptrParent;

		// default constructor
		Node();

		// constructor with object reference
		Node(T &object);

		// Copy constructor
		Node(Node<T> &copyNode);
	};

	// Template class. Sets pointers to NULL.
	// Note: You must add value manually.
	template<class T>
	Node<T>::Node()
	{
		ptrLeft = NULL;
		ptrRight = NULL;
		ptrParent = NULL;
	};

	// Template class. Sets value and nulls pointers.
	template<class T>
	Node<T>::Node(T &object)
	{
		value = object;
		ptrLeft = NULL;
		ptrRight = NULL;
		ptrParent = NULL;
	};

	//// Template copy constructor
	template<class T>
	Node<T>::Node(Node<T> &copyNode)
	{
		value = copyNode.value;
		ptrLeft = copyNode.ptrLeft;
		ptrRight = copyNode.ptrRight;
		ptrParent = copyNode.ptrParent;
	};
}
#endif // NODE_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// INFO450.h

#ifndef INFO450_H_
#define INFO450_H_

// see: http://support.microsoft.com/kb/168958

//disable warnings on 255 char debug symbols
#pragma warning (disable : 4786)
//disable warnings on extern before template instantiation
#pragma warning (disable : 4231)
#pragma warning (disable : 4251)

#include <vector>
#include <string>
#include <cstring>
#include <list>

#include "Node.h"


using namespace std;

// #define EXP_STL

// You must define EXP_STL when compiling the DLL.
#ifdef EXP_STL
#    define DECLSPECIFIER __declspec(dllexport)
#    define EXPIMP_TEMPLATE
#else
#    define DECLSPECIFIER __declspec(dllimport)
#    define EXPIMP_TEMPLATE extern
#endif

#pragma region Node
// Node<T>
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<char>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<vector<char>>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<list<char>>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<short int>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<vector<short int>>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<list<short int>>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<int>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<vector<int>>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<list<int>>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<long int>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<vector<long int>>;
EXPIMP_TEMPLATE template class DECLSPECIFIER INFO450::Node<list<long int>>;

// you get the idea...

// end Node<T>
#pragma endregion

#endif // INFO450_H_


Is it possible to import these classes using P/Invoke? If so, how do you go about it?

Thanks.
Topic archived. No new replies allowed.