|
I have seen examples of C++ unmanaged classes in extension DLLs, wrapped successfully with managed C++ classes, also in their own DLL, so that the managed classes can be called from C#. However, every example I have seen uses an unmanaged class that does not use any MFC. For example, I cannot find a single example (and have so far failed to create one myself) of an unmanaged C++ class that has some functions with CString arguments and/or return values, wrapped by a managed C++ class in a DLL, callable from C#. If anyone has such an example, I'd be forever grateful to receive it. When I try it, IJDW (it just doesn't work). I can get everything to compile file, but when I try to pass a string from the C# program down through the MC++ DLL to the unmanaged C++ DLL, I get an exception down there at the bottom saying that the object reference is not set to an instance of an object, even though I can see that the passed in string is just fine.
Thanks for any assistance.
|
|
|
|
|
Unfortunately, I don't use MFC in my coding projects, so my advice will be somewhat incomplete. We get this problem alot in Interop operations when passing data (often strings or structs) to native pre-.NET C++ functions in compiled DLLs.
The basic problem is the way the Interop marshaler processes the data; the default behavior has limited applicability with user-defined data structures. This is particularly a problem in COM, as many of the data structures are defined as types that have no compatible type in .NET.
The solution in .NET is to use attributes to control the marshaling behavior of the Interop when the default behavior is inappropriate, as in this case.
I suggest that you review the MarshalAs(UnmanagedType) attribute documentation over at MSDN. In all honesty, I don't what the correct usage would be, but, hopefully you won't have to write a custom marshal routine (because it's such a pain in the ass).
Try reading this: Deafult Behavior for Strings:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondefaultmarshalingforstrings.asp[^]
..or, this, the MarshalAs(UnmanagedType) Enumeration:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesunmanagedtypeclasstopic.asp[^]
|
|
|
|
|
I declared a few strings for a managed class:
public __gc class class1
{
public:
// function
String *string1;
String *string2;
};
Then in the contructor, I allocate memory for those string:
class1::class1()
{
string1 = new String("");
string2 = new String("");
}
But when I use them, the program is not stable, sometimes it just throw an exception on those string. I think I need to allocate enough memory for those string. How do I allocate memory for a string with specify length?
Thanks
|
|
|
|
|
I created a c++ object and exported the funtion I wanted to use with dllexport. Then created a managed wrapper that linked with my unmanaged
code. I used c# Windows application that made use of the managed wrapper.
All this works fine. Yipee
Now i am trying to export the entire c++ object. SO i have
//------------------------------------------------------
class __declspec(dllexport) Sum
{
public:
Sum();
~Sum();
public:
double Add( double num1, double num2 );
};
//------------------------------------------------------
Is it possible to export the entire class.
Now the MC++ was
//------------------------------------------------------
extern "C"
{
double Add( double num1, double num2 );
}
//--namespace stuff and other declaration
__gc class Class1
public: double DoAdd( double n1, double n2 )
{
return Add( n1, n2 );
}
//------------------------------------------------------
My question is what do I replace the extern "C" part with now and assuming that I dont have the source code for the c++ code, just the dll
and lib files.
|
|
|
|
|
Hi everybody, I'm a student, and I'm triying to create the tree command like int DOS, to print all the directories and files of my hard disk, but I can't .I must use "findfirst" and "findnext" function in Turbo C++ (3.0).
Please, help me!!! doh:
|
|
|
|
|
Well, I think I'd be right in saying that Turbo C++ does not support managed C++. You should find a forum that supports your compiler ( although from memory, firdfirst and findnext are Windows API functions, which means it shouldn't matter ). Your solution will be recursive, it will call itself whenever it finds a folder to move into.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
I've created MFC Application using Visual Studio 2003, then change the project settings
to use Managed Extensions as follows:
1. Configuration Properties->General->use Managed Extension : Yes
2. C/C++->General->Debug Information Format : Program Database (/Zi)
3. C/C++->Code Generation->Enable Minimal Build : No
4. C/C++->Basic Run-Time Check : Default
then in the CMyClassDlg.cpp did the following:
#using <mscorlib.dll>
#using <system.dll>
using namespace System;
#using <system.xml.dll>
#using <system.data.dll>
and in the OnInitDialog() did :
XmlDocument *xmlDoc = new XmlDocument();
//Just this to try
and compile this error message appeared:
(105): error C3828: 'System::Xml::XmlDocument': placement arguments not allowed while creating instances of managed classes
I dont know why this error generated ,though I did the same thing in my ISAPI Filter DLL project and used XmlDocument
and SqlClient effectively.
why this happening in MFC application .
Any Idea? Regards.
|
|
|
|
|
this the directive the check box for using < as HTML was not checked
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
#using <System.Xml.dll>
#using <System.Data.dll>
|
|
|
|
|
Hi!
I need to get the names and parameters of the functions out of an compiled dll.
thanks in advance
thomas
|
|
|
|
|
You should be able to read the library with Visual Studio.NET or the IL Disassembler. (I'm assuming that it was compiled with a .NET compiler.)
|
|
|
|
|
I've asked this question a while back and couldn't seem to get a straight answer. I want to make a MFC project using managed extensions. If I have a CFormView based form as my main window, can I add a windows form to my project and show it modal/modeless from an event in my main form (like pressing a button)? I've added a windows form to my project, but now I don't know how to make it display. Does anyone have any suggestions? Any help is appreciated. Thanks
while(true)
{
TryToLearn("C++,MFC");
}
My articles[^]
bdiamond
|
|
|
|
|
I've migrated an existing function from a mex file (previously used with Matlab) into a .c file and incorporated it into an existing MFC application (I compile it with the rest of the files, not as a separate library).
For some reason, that specific function runs much slower than it does when I run the similar mex version from Matlab. It has 7 nested 'for' loops which access different locations in an ~10300 long float array.
Is there a way to optimize the running time?
|
|
|
|
|
maybe if you put the code here i can help you, anyway 7 nested loops sounds very huge, i think that mathlab does a optimization maybe or a prebuffer...
|
|
|
|
|
I am porting my application which is developed in VC++ 6.0 to VC++ 7.0.
I got ambigous error when I am using
CString cstrTemp += "A";
I think CString class is modified in .net and It won't accept "+=" operator. What is the other alternative.
Thanks in advance.
|
|
|
|
|
Anonymous wrote:
CString cstrTemp += "A";
cstrTemp = "A" + what ? Get rid of the +, and use it only when the string you're trying to add to already exists.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Thanks.
Sorry for that error.
It is like this
CString strTemp = "Class";
strTemp += "A";
let me know how to solve this.
|
|
|
|
|
What's wrong with strTemp = strTemp + "A"; ?
Although I'll admit, it's a bit weird. What's the text of the error ?
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Should be:
CString str("A");
str += "B";
|
|
|
|
|
I have an unmanaged DLL,I want to convert this to a Managed code so that C# and VB can access the resulting managed DLL, we decided to use Managed Extensions for C++(MC++)
As i am facing difficulties in analyzing the concepts hidden with in MC++,i want to know as
1.how to take the existing unmanaged code and compile it to managed code, (with the /clr compiler switch and It Just Works (IJW) )
2.Also to write managed wrappers around unmanaged functions.
After going through articles this is what i have understood, but absolutely don't know whether it is right or wrong??
To create a blank solution in Visual Studio and add the following projects
1) Unmanaged (old dll)
2) Managed (dll written using MC++)
3) and finally the C# project which accesses this Managed project
If this is correct, then is it possible for me to
1) directly take my old MFC DLL and open it in Visual Studio
2) should i add the .cpp and .h files from old dll to the managed project
3) should the import library of old dll be added to linker dependancies in the managed project
if all the above mentioned steps are wrong then what are the detailed steps i am supposed to follow.Anybody please help me
thanks in advance
|
|
|
|
|
I'm not sure about with a dll, but for a regular project, I just made a copy of my project's entire folder (just for backup purposes) and opened the .dsw file from .Net. I think it tries to convert everything as best as it can, but I think you have to use pragma_push and pragma_pop macros for MessageBox() and new (and probably a few more that I may not have had in my program).
Who are all these people and what are they doing in my house?...Me in 30 years, inside a grocery store
My articles[^]
bdiamond
|
|
|
|
|
Hi,
I'm writing a designer for a component, this designer is written in managed c++ and it is linked to a native dll, when i try to inherit from this component to use the designer the visual studio designer view, show this error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: Class1 --- The base class 'GuiSystem.MGUIDialog' could not be loaded. Ensure the assembly has been referenced and compiled.
i think that this error because it can't find the designer dll the designer dll is linked to but i don't know how can i make it search for it in the correct path (the native dll is linked to other native dlls and they are all in the same folder with the manged designer dll).
thx
|
|
|
|
|
hi
I think it needs to reference the lib file for this dll. In that case you need to specify it in the Properties|Linker|Input|Additional dependencies for the project.
The dll needs to be in the debug/release folder, but this should get export automatically if the dependency for the projects are set correctly.
Hope this helps, but I could be wrong.
|
|
|
|
|
I'm trying to manipulate a string in a function and return the result in two subsequent arguements in the same call. Here is one version I've tried using de-referenced pointers.
In MFC I passed the 2nd and 3rd arguements in as references to the original CStrings and that worked fine.
The same approach does not work and regardless of how I do it, verb and arg in the calling function (decodeLine(..)) remain unchanged.
UInt32 verbArguement(String * line, String ** pVerb, String ** pArg)
{ // splits verb(args) into verb and (args)
int bracketPos;
String * verb = *pVerb;
String * arg = *pArg;
// pull off the word preceeding the (
bracketPos = line->IndexOf('(', 0);
if (bracketPos >= 0)
{
verb = line->Substring(0,bracketPos);
arg = line->Substring(bracketPos); // here verb == "hint" and arg == "(2,P)"
return 1;
}
else
{
fileReaderException * e = new fileReaderException(fileReaderException::ERR_NON_FILE);
throw e;
}
return 0;
}
// strLine == S"hint(2,P)"
Int32 decodeLine(String * strLine)
{
String * verb;
String * arg;
if (verbArguement(strLine, &verb, &arg))
{
// at this point verb == null and arg == null
}
}
It seems that Microsoft has done a cheap and dirty on the implementation of String, effectively making all methods constructors and letting the garbage collector clean up the mess. That is why the pass by reference approach did not work. It also seems that the semantics of pointers and de-referencing has also changed. I'm pretty sure that the above code would work in standard C++.
Does anyone have a solution?
|
|
|
|
|
|
I think it causes something to only be included once.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|