|
Hi,
I want to convert CString array to managed code ot send it to C#.
For normal CString i did like this,
CString menu = "MENU";
String ^ msg = gcnew String(menu);
Globals1::gwtoolbar->Add(msg);
But now i want to send array of string.i dont know how to do for CString array.
When i gave like this it shows error
CString menu[10];
String[] ^ msg = gcnew String(menu);
How can i convert it?Anu
|
|
|
|
|
I think you need to use the managed array ; see the documentation here[^] for some samples.txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
You would have to do something like this:
array<String^>^ managedArray = gcnew array<String^>(5);
CString nativeArray[5];
for (int i = 0; i < 5; i++)
{
managedArray[i] = Convert::ToString(i);
nativeArray[i] = gcnew String(nativeArray[i]);
}
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
gcnew System::String(char *)
|
|
|
|
|
Try this once
CString menu[5];
menu[5]={"1","2","3","4","5"};
String[] ^ msg = gcnew String(menu);
msg += " (System::String)";
|
|
|
|
|
hi, i have a problem when calling from the managed code the function
capGetDriverDescription(
WORD wDriverIndex,
LPTSTR lpszName,
INT cbName,
LPTSTR lpszVer,
INT cbVer
);
from avicap32.dll. the function is described in detail in http://msdn.microsoft.com/en-us/library/dd756909%28VS.85%29.aspx[^]. Basically it writes the name and description of videoinput device number wDriverIndex to the passed strings lpszName and lpszVer which have length cbName and cbVer respectively (better put, to the corresponding memory). the relevant code is the following
-----------------------------------------
[DllImport("avicap32.dll")] extern "C" bool capGetDriverDescription(UInt16, String^, int, String^, int);
.....
int i_nameLen = 100;
int i_verLen = 100;
String^ s_name = gcnew String('x',i_verLen);
String^ s_ver = gcnew String('x',i_verLen);
result = capGetDriverDescription(x, s_name, i_nameLen, s_ver, i_verLen);
------------------------------------------
the function returns for x=0 true and for x=1 false which is expected since only one webcam is attached to my computer. but the returned strings s_name and s_ver are unchanged. probably managed and unmanaged code dont use the same memory which makes it somewhat difficult to pass pointers.... what is the alternative?
btw, its my first time calling unmanaged code from managed one, so probably the answer will be quite obvious. thanks in advance!
david
ps: is there a smarter way to allocate memory to the strings? (probably yes...)
|
|
|
|
|
To use a string as an output parameter you should use the StringBuilder class instead of String .
[DllImport("avicap32.dll"),CharSet=CharSet.Unicode]
extern "C" bool capGetDriverDescription(UInt16, StringBuilder^, int, StringBuilder^, int);
But in C++/CLI you can directly call the native C function without use P/Invoke: you can mix managed and unmanaged code.
Hope it helps!
|
|
|
|
|
Hello , why i cant return the char* something using the class like that.
struct random_class {
char *something;
void __thiscall classfunction (void *pParam);
};
random_class* pParam;
void (__thiscall* classfunction)(void *pParam );
void __thiscall myclassfunction(void *pParam )
{
classfunction(pParam->something);
}
error C2227: left of '->crypto_buf' must point to class/struct/union/generic type
1> type is 'void *'
|
|
|
|
|
nah1337 wrote: ->crypto_buf'
are you sure you've posted the correct section of code ? I cant see :-
nah1337 wrote: ->crypto_buf'
anywhere in that snippet you've posted, so I dont know what relevance it has to your issue ...
'g'
|
|
|
|
|
Yeah was mine mistake it should look like this:
struct random_class {
char *crypto_buf;
void __thiscall classfunction (void *pParam);
};
random_class* pParam;
void (__thiscall* classfunction)(void *pParam );
void __thiscall myclassfunction(void *pParam )
{
classfunction(pParam->crypto_buf);
}
error C2227: left of '->crypto_buf' must point to class/struct/union/generic type
1> type is 'void *'
|
|
|
|
|
See my answer here[^]. txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
In unmanaged c++ dll i have a function which takes constant std::string as argument
Prototype : void read ( const std::string &imageSpec_ )
I call this function from managed c++ dll by passing a std::string.
When i debug the unmanaged c++ code the parameter "imageSpec_" shows the value correctly but does not allow me to copy that value in other variable.
imageSpec_.copy( sFilename, 4052 );
It shows length of "imageSpec_" as 0(zero).
If i try copying like " std::string sTempFileName(imageSpec_);" this statement string new string is a empty string.
But for "std::string sTempFileName(imageSpec_.c_str());" this statement string gets copied correctly. i.e. with charpointer string is copied correctly.
Copying this way will need a major change in unmanaged c++ code.
I am building unmanaged code in Visual studio 6.0 and managed c++ in Visual studio 2008.
Is there any specific setting or code change in managed c++ that will solve the issue?
|
|
|
|
|
i have not a real answer but it is really better to build all components with the same version of the Visual Studio. Please dont dicuss it. And VS6 is outdated.
Another thing: it is bad style if not crappy to make complex data types as parameters between components. Use const char * and maybe the length.
Maybe it is the solution Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
No, but you can covert System::String or std::string to char * on the managed C++ side.
|
|
|
|
|
#include "stdafx.h"
#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
using namespace std;
void mm(int**a,int **b,int **c){
__declspec(align(16)) int at[4][4],bl[4][4];
for(int j=0; j<4; ++j)
for(int i=0; i4; ++i){
at[j][i] = a[i][j]; //transpose 1st operand
bl[j][i] = b[j][i]; //local aligned non aliased
}
for (int j = 0; j < 4; ++j) //return matrix multiply4x4
for (int i=0; i < 4; ++i)
c[j][i] =inner_product(&at[i][0],&at[i][4],&bl[j][0],0.f);
}
my question: when compilling these codes it compiles well. but how can i initialise the two matrices so that i can view the output: assume a[4][4]={1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4} and b[4][4]={3,3,3,3,5,5,5,5,6,6,6,6,7,7,7,7}.please help.i am using MS VISUAL STUDIO 2008smart lubobya
|
|
|
|
|
Hello All,
I'm working on a managed wrapper for an unmanaged C++ library, and would like to use the objects generated by the managed wrapper as a data source within a Windows Forms Application.
For compatibility reasons, I'm using Visual Studio 2005.
The managed wrapper is using C++ interop to connect to the unmanaged library.
The problem I'm running into is that as soon as I add any code to the wrapper which actually interfaces to the unmanaged library, the managed wrapper's assembly disappears from the "Add New Data Source..." -> "Object" wizard.
I'm guessing here, but it appears that as soon as my wrapper's assembly contains any native code, it can no longer be used as a DataSource.
So, my question is, is it possible to implement a DataSource in a mixed-mode DLL?
Thanks in advance,
Ron Aldrich
|
|
|
|
|
Ron Aldrich wrote: the managed wrapper's assembly disappears from the "Add New Data Source..." -> "Object" wizard.
Is it coming when you add again?
I think it is not a good idea to use the mixed-mode assembly in data binding. Can't you wrap it inside another type and use that for binding? So that changes on the mixed-mode assembly won't affect the binding.Best wishes,
Navaneeth
|
|
|
|
|
Navaneeth wrote: Is it coming when you add again?
It appears that as long as there is any native code in the assembly, the entire assembly disappears from "Add New Data Source...", even though the native code is generated by C++ interop. If I then remove the native code, the assembly reappears in "Add New Data Source...".
Navaneeth wrote: I think it is not a good idea to use the mixed-mode assembly in data binding. Can't you wrap it inside another type and use that for binding? So that changes on the mixed-mode assembly won't affect the binding.
I'm beginning to think that it's not only not a good idea, it's actually impossible. I think that it can be made to work using p/invoke, but C++ interop makes it fairly easy to convert between the std::string* and std::vector* objects emitted by the native library, and the System::String^ and System::Collections::ArrayList^ objects used within managed code.
At this point, I'm looking at either
1) Redesign the native library's API to emit structures that are more easily marshaled using p/invoke, or
2) Add yet another layer of wrappings, just to allow the use of bindings.
From the standpoint of coding time, the second approach is probably simplest.
Thanks for your time,
Ron Aldrich
|
|
|
|
|
An interface in C++/CLI can have operators as static functions of the interface. If I code an implicit cast operator for an interface the compiler accepts it:
// VC9ClrConsoleApplication.cpp : main project file.
#include "stdafx.h"
using namespace System;
interface class AnInterface
{
static operator int (AnInterface ^ af) { return af -> IntReturn(); }
int IntReturn();
};
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Console::ReadKey();
return 0;
}
This compiles with no errors.
If I change my operator from an implicit cast to an explicit cast I get a C2071 illegal storage class error:
// VC9ClrConsoleApplication.cpp : main project file.
#include "stdafx.h"
using namespace System;
interface class AnInterface
{
static explicit operator int (AnInterface ^ af) { return af -> IntReturn(); }
int IntReturn();
};
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Console::ReadKey();
return 0;
}
".\VC9ClrConsoleApplication.cpp(10) : error C2071: 'AnInterface::operator int' : illegal storage class".
I can so no reason why an implicit cast operator is allowed for an interface and an explcit cast operator is not allowed for an interface.
For an implicit cast operator one would be able to say:
AnInterface ^ ai = CreationFunctionFromClassImplementingInterface();
int i = ai;
For an explicit cast operator, if it worked with interfaces, one would be able to say:
AnInterface ^ ai = CreationFunctionFromClassImplementingInterface();
int i = static_cast<int>(ai);
Is this a bug or is it, for some reason I can not fathom, just the way C++/CLI is supposed to work.Edward Diener
|
|
|
|
|
There's no mention in the standard about this behavior, so it could technically be regarded as a bug I suppose.
Why don't you replace your interfaces with abstract base classes? Or retain the interface and then add an abstract base class to the hierarchy. It's not ideal but that should solve all these operator overloading related weirdness you are experiencing.
|
|
|
|
|
I reported this as a bug at https://connect.microsoft.com/VisualStudio/feedback/details/539309/interface-operator-bug-with-explicit-conversion-function[^].
Using an abstract base class, as opposed to an interface, means that I can not derive my own eventual class from any other class, but I am sure you know that. Also I can not derive my interfaces from that abstract base class. All in all, wrenching a design in order to please Microsoft's inability to correctly implement operators in interfaces is not my idea of correct programming.
Microsoft needs to get this right, not only in operators in interfaces in C++/CLI but clearly they should add operators in interfaces in C# also. Else they are nearly as bad as Java, which doesn't believe in user-defined operators at all ( though Scala does to Java's detriment ).
But what's the point of telling Microsoft obvious things which their own developers are not intelligent enough to know.Edward Diener
|
|
|
|
|
How to use
BACKGROUNDWORKER in vc++ clr net 2.0 need example
I copy backgroundworker sample code in c# and try to conver to c++ clr it doesnt't in vc++ correct ly can anyone help ??
thanks
|
|
|
|
|
|
Hi All
How can i use .net dll in vc++?I make dll in .net and want to use in vc++?
Please help me
|
|
|
|
|
See this article[^]. txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|