473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Still need sample code for access to Win32 DLL

Specifically I need to wrap an older Win32 DLL in a managed class. I
had this running with VS2003's Managed Extensions, though it required
two separate classes.

With C++/CLI this was supposed to be simple, right? I had thought
that the new C++/CLI syntax was supposed to allow mixing of
managed/unmanaged in the same class but I'm running into quite a few
snags.

I'm sure the problems are commonplace, so rather than listing tons of
error messages, I'd like to get a good look at running sample code
that does this. Any leads?.
Mar 4 '06 #1
8 1747
Specifically I need to wrap an older Win32 DLL in a managed class. I
had this running with VS2003's Managed Extensions, though it required
two separate classes.

With C++/CLI this was supposed to be simple, right? I had thought
that the new C++/CLI syntax was supposed to allow mixing of
managed/unmanaged in the same class but I'm running into quite a few
snags.

I'm sure the problems are commonplace, so rather than listing tons of
error messages, I'd like to get a good look at running sample code
that does this. Any leads?.


check out the thead 'Pointers and references VS2005' in this newsgroup,
dated 1 march.
Someone else had problems consuming a VC6 dll in C++/CLI.

I posted a working example. maybe this can help you too?
www.codeproject.com could have something as well.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 4 '06 #2
[re: Wrapping DLLs with C++/CLI]

On Sat, 4 Mar 2006 17:48:03 +0100, "Bruno van Dooren"
<br**********************@hotmail.com> wrote:
I need to wrap an older Win32 DLL in a managed class. I
had this running with VS2003's Managed Extensions, though it required
two separate classes.
...
check out the thead 'Pointers and references VS2005' in this newsgroup,
dated 1 march.
Someone else had problems consuming a VC6 dll in C++/CLI.
I posted a working example. maybe this can help you too?
Thanks for that, Bruno. That will help elsewhere, but in this case,
the error messages are generated by the 'ref class' wrapper.
www.codeproject.com could have something as well.


I couldn't find anything on Codeproject.

OK, here are some specifics:

The unmanaged DLL has a corresponding .H file with typedefs for custom
structs etc:

in typedef_header.h:

typedef struct {
void *Handle;
unsigned long ULong;
} CustomType;

Some of those custom datatypes need to be accessed by several wrapped
functions in the managed wrapper class, so they must be exposed.

in the main C++/CLI wrapperclass.cpp:

#include "typedef_header.h"

public ref class WrapperClass {
char *CharArray;
CustomType Data;
};

Compiler Error:
error C4368: cannot define 'CustomType' as a member
of managed 'WrapperClass': mixed types are not supported

No problem with char* CharArray, though I'm not sure how to keep it
pinned.

Again, this was workable in VS2003 Managed Extensions, but required
two wrapper classes (unmanaged wrapped by managed). I'd like to get
this condensed into one C++/CLI container class, but I'm not sure how
to contain the unmanaged structs.

I'm sure I'm missing the obvious, and this must be covered somewhere.
I just can't find references.

Mar 5 '06 #3
error C4368: cannot define 'CustomType' as a member
of managed 'WrapperClass': mixed types are not supported


I found the answer here:
http://msdn2.microsoft.com/en-us/library/xhfb39es.aspx

in C++/CLI you cannot embed native types in managed types, but you can use
pointers to them.

a solution for your problem could be to use a pointer for which you
dynamically allocate the space in your constructor, instead of embedding
your type directly. obviously you have to delete the pointer in the
destructor of your ref class.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 5 '06 #4
On Sun, 5 Mar 2006 14:01:06 +0100, "Bruno van Dooren"
<br**********************@hotmail.com> wrote:
error C4368: cannot define 'CustomType' as a member
of managed 'WrapperClass': mixed types are not supported


I found the answer here:
http://msdn2.microsoft.com/en-us/library/xhfb39es.aspx

in C++/CLI you cannot embed native types in managed types, but you can use
pointers to them.

a solution for your problem could be to use a pointer for which you
dynamically allocate the space in your constructor, instead of embedding
your type directly. obviously you have to delete the pointer in the
destructor of your ref class.


I was afraid you were going to say that, Bruno. <g> I had come to the
same conclusion but was hoping I was missing something. There are so
many of those custom types that it's probably not worthwhile to go
with the conventional C++/CLI approach at all.

The main point of the inner unmanaged wrapper class (in the old
Managed Extensions version) was to isolate the awkward data types.
Requiring allocs for all those tiny structs would make things worse
rather than better, and would leave the managed code interspersed with
allocs and frees.

I guess I'm back to something like the old Managed Extensions
approach. I still may be able to move forward a tiny bit. This time
I could use the /cli compile switch, with the inner wrapping class
flagged as #pragma unmanaged, isolating the handles and awkward data
types. I think that will remove some of the ugly Managed Extensions
__whatever, leaving the code close to native. The outer class could
be a straight 'ref class' in managed code, calling into the inner
native class.

This would still look like ME code though, and would require two
function calls to get through the managed/unmanaged wrappers to the
inner DLL. I've found that to be error-prone. Still better than a
zillion allocs and frees.

I was looking forward to C++/CLI coming into its own, but I guess I
can't make use of it.

Another related question: Is there any way to use different compile
flags (/cli vs /cli:old....) on different files in one project?

Mar 5 '06 #5
> I was looking forward to C++/CLI coming into its own, but I guess I
can't make use of it.
For what it's worth, this topic came up in the newsgroup a couple of weeks
ago, and I seem to recall someone from the VC compiler team saying that the
reason it wasn't yet implemented was the underlying complexity.
It is possible that this will still be implemented in a later VC version.
Another related question: Is there any way to use different compile
flags (/cli vs /cli:old....) on different files in one project?


I just did a quick test: while it is possible to specify /clr:oldSyntax for
StdAfx.cpp and /clr for main.cpp,
you get a lot of compilation errors so I don't think it will work.
An alternative would be to put all that interop stuff in one class lib that
you compile with oldSyntax, and then use that classlib in a C++/CLI project.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 5 '06 #6
On Sun, 5 Mar 2006 21:59:04 +0100, "Bruno van Dooren"
<br**********************@hotmail.com> wrote:
I was looking forward to C++/CLI coming into its own, but I guess I
can't make use of it.


For what it's worth, this topic came up in the newsgroup a couple of weeks
ago, and I seem to recall someone from the VC compiler team saying that the
reason it wasn't yet implemented was the underlying complexity.
It is possible that this will still be implemented in a later VC version.


Now that I think about the implications, that would be tough. Still,
it leaves a problem with encapsulating complex native code.

The typedef'd data is the main trainwreck for me. I'll need to resort
to the old unmanaged-wrapper-inside-a-managed-wrapper routine that
was used with managed extensions.

Given that the outer (managed) class will have to maintain a pointer
to the inner (unmanaged) class, a couple more questions arise:

Since the outer class has to create the unmanaged inner class, are
there any problems in the unmanaged code being affected by the memory
manager? I would think that simply doing this would work:

Class OuterClass {
private:
InnerClass * InnerPtr;
....
}

// Constructor
OuterClass::OuterClass()
{
InnerPtr = new InnerClass();
}

What, if anything, needs to be pinned? Is everything that's allloc'd
via 'new' safe from the memory manager? I have to make sure that
everything in the inner class stays put.

Mar 6 '06 #7
> Since the outer class has to create the unmanaged inner class, are
there any problems in the unmanaged code being affected by the memory
manager? I would think that simply doing this would work:

Class OuterClass {
private:
InnerClass * InnerPtr;
....
}

// Constructor
OuterClass::OuterClass()
{
InnerPtr = new InnerClass();
}

What, if anything, needs to be pinned? Is everything that's allloc'd
via 'new' safe from the memory manager? I have to make sure that
everything in the inner class stays put.


I don't think anything should be pinned at all.
the fact that you use new (at least in C++/CLI) makes that InnerClass is
allocated on the normal heap.
the only thing that is managed is the pointer variable itself.

I.e. if you want to use &InnerClass in an unmanaged function you need to to
something like

UnManagedUseHandle(InnerClass ** InnerHandle); //example function
pin_ptr<InnerClass*> handle = &InnerPtr; //ping ptr
to ptr
UnManagedUseHandle(handle); //use ptr
to ptr

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 6 '06 #8
On Mon, 6 Mar 2006 21:45:14 +0100, "Bruno van Dooren"
<br**********************@hotmail.com> wrote:
iycrd writes:
Since the outer class has to create the unmanaged inner class, are
there any problems in the unmanaged code being affected by the memory
manager?
I don't think anything should be pinned at all.
the fact that you use new (at least in C++/CLI) makes that InnerClass is
allocated on the normal heap.


That's what I thought, but the memory manager makes me nervous about
that. <g> I wanted to make sure I hadn't misssed anything.

Bruno, you're a one-man team. Thanks for following up.

Mar 7 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ReaprZero | last post by:
Hi, I'm using Cygwin and ActiveState perl to try to compile a sample application using SWIG. I'm using the short tutorial from http://www.swig.org/tutorial.html (the perl part of it), but with a...
5
by: Ken Varn | last post by:
I have a named mutex object that is accessed by both an asp.net application and a Windows executable .net application. The Windows executable runs under the administrator logon, while the asp.net...
3
by: Mike S | last post by:
Anyone know how I can access a MS Access database from perl? Any help appreciated.
19
by: Randy | last post by:
Hi, I need to create a simple dll which will be used by MS Access and .ASP apps. What type of dll would be best and can anybody point me to a complete example which I can compile and run with...
6
by: dbuchanan | last post by:
I have a Windows Forms application that accesses SQL Server 2k from a small local network. The application has been used for weeks on other systmes but a new install on a new machine retruns...
0
by: Ray Tayek | last post by:
hi, getting a: .\Stdafx.cpp : fatal error C1192: #using failed on 'i: nunit\samples\cpp-sample"' 'The filename, directory name, or volume label syntax is incorrect.' nunit is installed in...
0
by: Budhi Saputra Prasetya | last post by:
Hi, I still have the same problem with embedding Windows Control. I'll just requote what I posted last time: I managed to create a Windows Form Control and put it on my ASP .NET page. I...
8
by: john | last post by:
To test a new piece of software designed to help with (among other things) eCommerce WWW site development. The software is fairly easy to use but you must fit a profile. Retail price is 120 GBP and...
5
by: Will | last post by:
- I know enough ASP and Access to be dangerous :) - I need to put up a data base on our web server with 3 related tables. - They will be accessed by a limited number of people. - Each user will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.