How to call a function in mfc...? - visual-c++

I am new to MFC PROGRAMMING. I use vs 2008, in a Dialog Based App. I want to call bellow function on a button click event...?
When I call like SortList(listboxone); is giving an error that SortList not found...!
Please help me..!!
void SortList(CListBox& templistbox)
{
DWORD_PTR abc;
int a=templistbox.GetCurSel();// Select current Item Index
if(a<templistbox.GetCount()-1)
{
abc = (DWORD_PTR )templistbox.GetItemData(a);
a++;
templistbox.SetItemData(a,(DWORD_PTR) templistbox.GetItemData(templistbox.GetCurSel()));
}
}
Sorry now I changed the function to as above but still gives same error.

You probably are calling the function above the function definition. In C/C++, you need to define the function (or it's prototype at least) before calling the function. Put this:
void SortList(CListBox& templistbox);
at the the top of the source file.

Related

Not able to add OnManipulationEnded via Script(MRTK)

I am trying to add the Manipulation event (OnManipulationEnded) via script.
Below is the event I wanted to add.
I am trying to add this via below script:
mainobject.AddComponent<ObjectManipulator>();
mainobject.GetComponent<ObjectManipulator>().OnManipulationEnded.AddListener(() => new_position());
But this is the error that I am getting:
CS1593 Delegate 'UnityAction' does not take 0 arguments.
I do not know what arguments I need to pass
I want a new position after Gameobject is manipulated from Position A to Position B.
I have created a new function to calculate the new Positions. I want this function to be called once Manipulation is ended.
I am very new to C# and Unity. Can someone please help?
Regards,
Mayank
Please refer to ManipulationEvent Class (Microsoft.MixedReality.Toolkit.UI) | Microsoft Learn. This event has the default callback argument ManipulationEventData. So, this argument must be included in your custom function. You can refer to Unity - Scripting API: UnityEvent (unity3d.com) and the following methods.
ObjectManipulator objectManipulator;
void Start()
{
objectManipulator = GetComponent<ObjectManipulator>();
objectManipulator.OnManipulationEnded.AddListener(TouchEnded);//1
objectManipulator.OnManipulationEnded.AddListener((data) => TouchEnded(data));//2
objectManipulator.OnManipulationEnded.AddListener((data) =>{ });//3
}
private void TouchEnded(ManipulationEventData arg)
{
}

Alternative for ON_COMMAND message map macro to call a function with a paramenter(s)

I've got a message map at the beginning of my program that looks like the following:
BEGIN_MESSAGE_MAP(SoftwareDlg, CDialog)
//{{AFX_MSG_MAP(SoftwareDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_DESTROY()
...
ON_COMMAND(ID_TOOLS_UPLOADDATA, UploadData)
...
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
And here the function that the ID_TOOLS_UPLOADDATA menu option calls when clicked:
void UploadData()
{
string apiEndpoint = "/api/stuff";
upload_data(apiEndpoint);
}
My problem is that that I want my UploadData() function to be able to take the string apiEndpoint as a parameter so that I can call it from multiple locations in my program for multiple operations (not just when the user clicks the menu button). Like so:
void UploadData(string apiEndpoint = "/api/stuff")
{
upload_data(apiEndpoint);
}
I took a look at ON_COMMAND_EX, but the only usage example I was able to find does not appear to be what I'm looking for... Anyone have any ideas?
As #Iinspectable said, you can’t.
Try for example to add a member variable and set it before calling the function.

Haxe 4: Uncaught exception macro-in-macro

I had code like this working with Haxe 3:
macro public static function get(key:String)
{
return Context.makeExpr(Context.definedValue(key), Context.currentPos());
}
However, after migrating to Haxe 4 this fails to compile with error:
Uncaught exception macro-in-macro
How should I go about migrating this function to Haxe 4? Is there a better way to access build flags in order to avoid this issue?
As #Gama11 alluded to, there's not actually a problem with your macro function, there's a problem with where you're calling it from. (Haxe 4 may have gotten more strict with these checks.)
If you have:
Main.hx
class Main
{
public static function main()
{
// Can call get from here:
var cvar = MacroUtil.get('cvar');
MacroUtil.some_macro_function();
trace('Hello world! cvar=${ cvar }');
}
}
MacroUtil.hx
import haxe.macro.Context;
import haxe.macro.Expr;
class MacroUtil
{
macro public static function get(key:String):Expr
{
return Context.makeExpr(Context.definedValue(key), Context.currentPos());
}
macro public static function some_macro_function()
{
// Cannot call get from here:
var cvar:Expr = get('cvar');
trace('will trace at compile time, and cvar is ${ cvar }');
return macro trace('will trace at runtime');
}
}
And execute it with: haxe -x Main -D cvar=abc
That will generate the error you're experiencing. It's because in some_macro_function, you're already in the macro context, so you can't call the macro function get from there.
There are a couple ways of dealing with this.
One Approach
You can use #if macro / if !macro to detect the macro context and adjust accordingly. So as silly as this looks, it does indeed solve your particular problem:
class MacroUtil
{
#if !macro macro #end public static function get(key:String):Expr
{
This function signature says, if I'm already in the macro context, don't consider this function a macro function. It's just a static helper at that point. And remember that it returns an Expr, not a String like it does in the main context.
If you mix macro and non-macro functions in a single file, you may also find yourself needing to use #if macro to avoid this condition as well.
Another Approach
You can refactor your macro functions into macro functions and macro helpers. It's a little more verbose, but maybe a little more clear as to what's happening:
MacroUtil.hx
import haxe.macro.Context;
import haxe.macro.Expr;
class MacroUtil
{
macro public static function get(key:String):Expr
{
return Context.makeExpr(MacroHelpers.get_define(key), Context.currentPos());
}
macro public static function some_macro_function()
{
// Cannot call get from here:
var cvar:String = MacroHelpers.get_define('cvar');
trace('will trace at compile time, and cvar is ${ cvar }');
return macro trace('will trace at runtime');
}
}
class MacroHelpers
{
public static function get_define(key:String):String
{
return Context.definedValue(key);
}
}
If you do it this way, then your macro functions all call the MacroHelpers, and non-macro function call the MacroUtils. Notice the helper returns a String, and it's up to the call-site to then convert it to an expression, if that's what they want.
We ended up removing this whole get method and switching occurrences to use Compiler.getDefine() instead, which is supported both by Haxe 3 and 4.
I believe the problem we were facing was related with the fact that this static macro get was being called from our test runner script, so that probably was the place where a macro was calling another macro. Still, I tried to put the solution suggested by Jeff Ward in place but kept getting the same result.

Replace Method name at run time dynamically every time i call in for loop C#

I want to call "Payment010" method with different every time i call, example at the iteration it suppose to be called as Payment010 and at the second iteration i wanted to call as Payment011, Is that possible?
Much appreciated for help in advance!
public class PaymentChargeCreditCard
{
public async Task Payment010()
{
await this.MakePayment(
orderNo,
shipDate);
}
}
C# reflection can call a function from string.
This might be what you want.
Calling a function from a string in C#

"this" argument in boost bind

I am writing multi-threaded server that handles async read from many tcp sockets. Here is the section of code that bothers me.
void data_recv (void) {
socket.async_read_some (
boost::asio::buffer(rawDataW, size_t(648*2)),
boost::bind ( &RPC::on_data_recv, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} // RPC::data_recvW
void on_data_recv (boost::system::error_code ec, std::size_t bytesRx) {
if ( rawDataW[bytesRx-1] == ENDMARKER { // <-- this code is fine
process_and_write_rawdata_to_file
}
else {
read_socket_until_endmarker // <-- HELP REQUIRED!!
process_and_write_rawadata_to_file
}
}
Nearly always the async_read_some reads in data including the endmarker, so it works fine. Rarely, the endmarker's arrival is delayed in the stream and that's when my program fails. I think it fails because I have not understood how boost bind works.
My first question:
I am confused with this boost totorial example , in which "this" does not appear in the handler declaration. ( Please see code of start_accept() in the example.) How does this work? Does compiler ignore the "this" ?
my second question:
In the on_data_recv() method, how do I read data from the same socket that was read in the on_data() method? In other words, how do I pass the socket as argument from calling method to the handler? when the handler is executed in another thread? Any help in form of a few lines of code that can fit into my "read_socket_until_endmarker" will be appreciated.
My first question: I am confused with this boost totorial example , in which "this" does not appear in the handler declaration. ( Please see code of start_accept() in the example.) How does this work? Does compiler ignore the "this" ?
In the example (and I'm assuming this holds for your functions as well) the start_accept() is a member function. The bind function is conveniently designed such that when you use & in front of its first argument, it interprets it as a member function that is applied to its second argument.
So while a code like this:
void foo(int x) { ... }
bind(foo, 3)();
Is equivalent to just calling foo(3)
Code like this:
struct Bar { void foo(int x); }
Bar bar;
bind(&foo, &bar, 3)(); // <--- notice the & before foo
Would be equivalent to calling bar.foo(3).
And thus as per your example
boost::bind ( &RPC::on_data_recv, this, // <--- notice & again
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)
When this object is invoked inside Asio it shall be equivalent to calling this->on_data_recv(error, size). Checkout this link for more info.
For the second part, it is not clear to me how you're working with multiple threads, do you run io_service.run() from more than one thread (possible but I think is beyond your experience level)? It might be the case that you're confusing async IO with multithreading. I'm gonna assume that is the case and if you correct me I'll change my answer.
The usual and preferred starting point is to have just one thread running the io_service.run() function. Don't worry, this will allow you to handle many sockets asynchronously.
If that is the case, your two functions could easily be modified as such:
void data_recv (size_t startPos = 0) {
socket.async_read_some (
boost::asio::buffer(rawDataW, size_t(648*2)) + startPos,
boost::bind ( &RPC::on_data_recv, this,
startPos,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} // RPC::data_recvW
void on_data_recv (size_t startPos,
boost::system::error_code ec,
std::size_t bytesRx) {
// TODO: Check ec
if (rawDataW[startPos + bytesRx-1] == ENDMARKER) {
process_and_write_rawdata_to_file
}
else {
// TODO: Error if startPos + bytesRx == 648*2
data_recv(startPos + bytesRx);
}
}
Notice though that the above code still has problems, the main one being that if the other side sent two messages quickly one after another, we could receive (in one async_read_some call) the full first message + part of the second message, and thus missing the ENDMARKER from the first one. Thus it is not enough to only test whether the last received byte is == to the ENDMARKER.
I could go on and modify this function further (I think you might get the idea on how), but you'd be better off using async_read_until which is meant exactly for this purpose.

Resources