|
Hi,
Currently I have this way implemented but I was hoping that there could just get around it, as subsiquent posts have revealed.
Thanks anyway,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
No problem. Hope all turns out well. I, too, like that code below. <smile />
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
To access element x, y in the returned float** , you should do this :-
*((float*)f + x*4 + y)
e.g.
float **f;
A id;
f = id.GetData();
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
cout << *((float*)f + i*4 + j) << " ";
}
cout << endl;
}
|
|
|
|
|
Hi,
Is this not the same as f[0][0] or do float** work differently?
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
you need to return a pointer to the array like the following. Note that I use a typedef for the array type as it makes returning a pointer to an array easier to read.
#include <stdio.h>
typedef float FixedFloatArray[4][4];
class A
{
public:
A()
{
for(int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
data[i][j] = (float)(i * 4 * j);
}
}
}
FixedFloatArray* GetData() { return &data; }
protected:
FixedFloatArray data;
};
void main()
{
A foo;
FixedFloatArray* data = foo.GetData();
printf("%f\n", (*data)[1][1]);
}
The basic problem with the way you were trying to do it is that the compiler doesn't know how big the rows of the array are at the GetData callsite. So, while the code you wrote looks "correct", it isn't because the compiler doesn't how to write code that will correctly address the elements of the array. Remember this when thinking that pointers are equivalent to arrays. They aren't.
|
|
|
|
|
Yep, your way is the right one, but if you know the length of the dimensions of the array, you can get at ecah element (as I've shown in my post).
|
|
|
|
|
|
another way to do this is to overload the array operator "[]" in the class like the following:
class A
{
float* operator[](int row)
{
return data[row];
}
};
void main()
{
A foo;
printf("%f\n", foo[1][1]);
}
|
|
|
|
|
That is quite cool, never thought of doing that
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Excellent,
Thank-you
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
TBD.
modified 28-Jan-14 19:18pm.
|
|
|
|
|
|
i am using a ddl to call other dll to show a form, that´s ok.But the problem is, i can not give the focus to the form, if there are more forms or programs opened the form that i call stay under the others , i want it to apears in front of all the others things opened,i have already used ShowModal but it does not work, can you help me, i am using C++ Builder(Borland) in windows 2000.
tks
|
|
|
|
|
This forum is for C++/CLI related questions. While none of the forums really handle C++ Builder queries, you could try the Visual C++ forum.
|
|
|
|
|
I want to develop a firewall software with C++ Builder.But i don't know how can i start.Must i use a driver or anything else (Ndis,filter hook ....)Please tell me how can i start? What i need ?
what is ndis and winpkfilter ?
|
|
|
|
|
I doubt that C++ Builder supports C++/CLI. (i.e. please don't flood the forums )
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I have two questions really bothers me.
1. Is it possible to have .Net assembly with global functions so that I can add reference and use them in my application by ::globalfunctions?
2. I have one small solution with two projects: one to create the dll and the other is to consume it. In the application, I can see the intellisense recognize the member in the namespace. But when I compile, I got error 2039 "something" is not a member of "namespace".
here is the sample code:
[the code for the dll]
Int32 sometfunction()
{
return 0;
}
namespace Cards
{
public __value int somethingnew;
}
[The code consume it]
#include "stdafx.h"
using namespace System;
using namespace Cards;
Int32 main(void)
{
Int32 ddd = ::somefunction();
Int32 dkd = Cards::somethingnew;
return 0;
}
Since I already added the reference to the application, there is no need to use something like
#using <cards.dll>
Any help will be greatly appreciated. I believe this is the right forum to get answer( I posted this question friday to another forum no one even care about what I asked )
Devin
|
|
|
|
|
devinzhang wrote: Is it possible to have .Net assembly with global functions so that I can add reference and use them in my application by ::globalfunctions?
I doubt it, seeing as .NET generally forces OO ( does not allow anything global ). That's why, for example, you have a Math class full of static functions instead of a math library like C++.
devinzhang wrote: Since I already added the reference to the application, there is no need to use something like
I don't believe that is correct. The intellisense often finds stuff even if you've failed to scope it with using statements or explicit use of namespaces.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thank you Christian for the reply ,
1. I doubt it, seeing as .NET generally forces OO ( does not allow anything global ).
But does that mean if I have the global functions or variables in my source file the generated dll won't actaully contain the information about global functions or variables? or even if the dll contains the information of global functions or variables, I still cannot retrieve it?
What could be the best solution? I have a project mixing of lots of global functions and classes. I need to export several global functions. I want to use them in .Net way by just adding reference instead of using Dllimport in the application.
1. I don't believe that is correct. The intellisense often finds stuff even if you've failed to scope it with using statements or explicit use of namespaces.
I see. Sorry for missing the words after #using in my previous post. I mean #using Cards.dll on the top is not necessary if you already have it added in reference. #using namespace is always necessary and I did use it in my code.
I can see the global functions I declared in the object browser of the project but not in the created dll that I added as reference.
Also, as you can see I declared somethingnew in the namespace Cards, but I cannot see it in the created DLL, did I do something wrong?
Devin
|
|
|
|
|
devinzhang wrote: But does that mean if I have the global functions or variables in my source file the generated dll won't actaully contain the information about global functions or variables? or even if the dll contains the information of global functions or variables, I still cannot retrieve it?
It probably means you can't use them from managed code. I'm not sure.
devinzhang wrote: I have a project mixing of lots of global functions and classes. I need to export several global functions. I want to use them in .Net way by just adding reference instead of using Dllimport in the application.
You'd have to ask someone like Nish, but I'd be surprised if you can use your global stuff that way.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I posted a related question the other day regarding statics. My current thought is that you need to use a delegate.
I have not had a chance to try this myself and given the problems I have been having todate I may be wrong. I also want to pass a static data member around. I tried using tools->ILDasm to see if its being exported but am at the bottom of the learning curve at the moment being new to microsoft.
This is the example from msn web site.
// use_delegate.cpp
// compile with: /clr
using namespace System;
ref class SomeClass
{
public:
static void Func(String^ str)
{
Console::WriteLine("static SomeClass::Func - {0}", str);
}
};
ref class OtherClass
{
public:
OtherClass( Int32 n )
{
num = n;
}
void Method(String^ str)
{
Console::WriteLine("OtherClass::Method - {0}, num = {1}",
str, num);
}
Int32 num;
};
delegate void MyCallback(String^ str);
int main( )
{
MyCallback^ callback = gcnew MyCallback(SomeClass::Func);
callback("single");
callback += gcnew MyCallback(SomeClass::Func);
OtherClass^ f = gcnew OtherClass(99);
callback += gcnew MyCallback(f, &OtherClass::Method);
f = gcnew OtherClass(100);
callback += gcnew MyCallback(f, &OtherClass::Method);
callback("chained");
return 0;
}
|
|
|
|
|
Barry, thank you for the code and the idlasm.
From the idlasm, I can see all the global functions are there. Actually, I can use dllimport to import those functions before. I just want a .Net way to import it.
I created a simple .Net wrapper class with several static methods corresponding to the global functions I want to export. The code sample is like
Int32 GlobalFunc1()
{
return 0;
}
Int32 GlobalFunc2()
{
return 0;
}
namespace MyNameSpace
{
public __gc class MyWrapper
{
public:
static Int32 GlobalFunc1()
{
return ::GlobalFunc1();
}
static Int32 GlobalFunc2()
{
return ::GlobalFunc2;
}
}
}
Then in the application side after you adding the reference
using MyNameSpace
Int32 main()
{
MyWrapper::GlobalFunc1();
MyWrapper::GlobalFunc2();
return 0;
}
Barry, this solves my problem. I am not sure if this helps you. If you want to use the class in your global functions, I think you can directly create an instance of your class without using delegate.
Devin
|
|
|
|
|
I have some problem working with LPCTSTR. I have a function which accepts some argument of type LPCTSTR like someFunction(LPCTSTR arg) and when I try to use switch sommand on this argument it generates an error telling that switch can't be used with LPCTSTR type. Code is something like this:
swith(arg)
{
//Some code ...
}
and even when I check it with "if" it doesn't work too. Suppose that arg value is something like "test" which I can see by using AfxMessageBox(arg)., although it shows that arg value is "test" but this line of code doesn't work:
if(arg == _T("test"))
//this code doesn't work! Although arg is "test"
what's the problem with that?
... And nothing else matters!
|
|
|
|
|
|
You're new to C/C++, aren't you?
Strings in C and C++ are pointers to arrays of characters, ended with a special character having the value zero. If you do this: if(arg == _T("test")) you're not comparing the string values but the pointers. In your case, the input parameter is most likely a pointer pointing to some string array in your process' data segment. The literal "test" is in a read only segment. Due to this fact, the two strings are at different memory locations, and therefore the addresses in your pointer and literal are not equal, hence the failing test.
What you must do in order to compare strings for value equality, is that you have to compare the characters in the memory areas. That is best done using functions such as strcmp et al.
--
Pictures[^] from my Japan trip.
|
|
|
|