I am trying to follow this example on how to create threads in a windows form. Purely following the example I get multiple syntax errors upon building.
I am using the /clr:oldSyntax compiler switch to compile the example.
Initialization of the Form1 class is the first source of the error:
public ref class Form1 : public System::Windows::Forms::Form
{
Errors:
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2143: syntax error : missing ';' before '{'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2447: '{' : missing function header (old-style formal list?)
Where exactly are these errors coming from?
Full Code:
#pragma once
using namespace System::Threading;
namespace ThreadExample
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::ProgressBar^ progressBar1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
private: Thread *trd;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(197, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(94, 162);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 23);
this->progressBar1->TabIndex = 1;
this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
ThreadStart *myThreadDelegate = new ThreadStart(this, repeat);
trd = new Thread(myThreadDelegate);
trd->IsBackground = true;
trd->Start();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox::Show(S"This is the main thread");
}
private: System::Void progressBar1_Click(System::Object^ sender, System::EventArgs^ e)
{
}
};
__delegate void DelegateThreadTask();
private: void ThreadTask()
{
int stp;
int newval;
Random *rnd=new Random();
if (progressBar1->InvokeRequired == false)
{
stp=this->progressBar1->Step*rnd->Next(-1,2);
newval = this->progressBar1->Value + stp;
if (newval > this->progressBar1->Maximum)
newval = this->progressBar1->Maximum;
else if (newval < this->progressBar1->Minimum)
newval = this->progressBar1->Minimum;
this->progressBar1->Value = newval;
}
else
{
DelegateThreadTask *myThreadDelegate = new DelegateThreadTask(this,ThreadTask);
this->Invoke(myThreadDelegate);
}
}
private: void repeat()
{
while(true)
{
ThreadTask();
Thread::Sleep(100);
}
}
}
The old syntax for C++/CLI uses __gc to declare classes.
References:
C++/CLI language specification from Visual Studio 2003, when the 'old' syntax was the only syntax.
VS2005 documentation giving both old & new syntax for declaring managed types in C++/CLI.
Test 1:
public ref class Foo
{
};
Compile result:
OldSyntax.cpp
OldSyntax.cpp(1): error C2059: syntax error : 'public'
OldSyntax.cpp(2): error C2143: syntax error : missing ';' before '{'
OldSyntax.cpp(2): error C2447: '{' : missing function header (old-style formal list?)
Test 2:
public __gc class Foo
{
};
Compile result: no errors.
(Note: I used VS2012 for my test. I don't have VS2005 installed anymore; the error messages may be slightly different on 2005.)
Related
I am trying to bind Jumio Natverify library in Xamarin iOS project by creating Objective c library project.
I have crated ApiDefination.cs and Struct.cs file data using sharpie tool. But when I am trying on run it I am getting Could not create an native instance of the type 'JumioNetverifyBinding.NetverifyConfiguration': the native class hasn't been loaded. exception.
ApiDefinition class - ApiDefinition.cs
Struct class - Struct.cs
Called NetverifyViewController from iOS Binding project:
using Foundation;
using JumioNetverifyBinding;
using System;
using UIKit;
namespace JumioNetverifyDemo
{
public partial class ViewController : UIViewController
{
NetverifyViewController netverifyViewController;
public ViewController(IntPtr handle) : base(handle) { }
public override void ViewDidLoad()
{
StartNetverifyButton.TouchUpInside += startNetverify;
}
public void CreateNetverifyController()
{
NetverifyConfiguration config = new NetverifyConfiguration();
config.ApiToken = "My_token_key";
config.ApiSecret = "Secrate_key";
config.DataCenter = JumioDataCenter.Eu;
config.Delegate = new NetverifyViewControllerDelegateHandler(this);
this.netverifyViewController = new NetverifyViewController(config);
}
public void startNetverify(object sender, EventArgs e)
{
this.CreateNetverifyController();
if (this.netverifyViewController != null)
{
this.PresentViewController(netverifyViewController, true, null);
}
else
{
Console.WriteLine("Netverify Mobile SDK : NetverifyViewController is null");
}
}
public void DisplayAlertAsync(string title = "Alert", string message = "")
{
var okAlertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(okAlertController, true, null);
}
public class NetverifyViewControllerDelegateHandler : NetverifyViewControllerDelegate
{
private ViewController _viewController;
public NetverifyViewControllerDelegateHandler(ViewController viewController)
{
_viewController = viewController;
}
public override void DidCancelWithError(NetverifyViewController netverifyViewController, NetverifyError error, string scanReference, string accountId)
{
Console.WriteLine("NetverifyViewController did finish initializing, error:" + error.Message);
_viewController.DisplayAlertAsync("Error : " + error.Message);
}
public override void DidFinishWithDocumentData(NetverifyViewController netverifyViewController, NetverifyDocumentData documentData, string scanReference, string accountId, bool authenticationResult)
{
_viewController.DisplayAlertAsync("Scan Reference : " + scanReference);
}
}
}
}
I am looking for anyway to handle this exception and make this library work, if anyone have idea about this? Any help would be appreciated.
I am very confused and frustrated, I am in an assembly programming class and am tasked with modifying an example from the textbook for one on my assignment problems. The example is a simple string encrypter which uses inline assembly to modify characters from an input string one-by-one and print it back out in another textbox within a windows form.
I wrote everything exactly as it is in the text and no dice Visual Studio will not build it and tons of build errors are thrown. I don't understand what I am doing wrong, and it is frustrating when I cannot practically apply something that is demonstrated in the text.
Here is the example code:
Windows Form Data Encryption
And here is what I have which is in the header file .h that is part of the form design:
#pragma once
char EncryptionKey = 0x45;
char Encrypt(char code)
{
_asm
{
mov a1, code
xor a1, EncryptionKey
mov code, a1
mov a1, EncryptionKey
inc a1
and a1, 7fh
mov EncryptionKey, a1
}
return code;
}
namespace TextEncryter {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ Encrypt;
protected:
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::RichTextBox^ richTextBox2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->Encrypt = (gcnew System::Windows::Forms::Button());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->richTextBox2 = (gcnew System::Windows::Forms::RichTextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// Encrypt
//
this->Encrypt->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->Encrypt->Location = System::Drawing::Point(197, 249);
this->Encrypt->Name = L"Encrypt";
this->Encrypt->Size = System::Drawing::Size(75, 23);
this->Encrypt->TabIndex = 0;
this->Encrypt->Text = L"Encrypt";
this->Encrypt->UseVisualStyleBackColor = true;
this->Encrypt->Click += gcnew System::EventHandler(this, &Form1::Encrypt_Click);
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(13, 22);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(259, 96);
this->richTextBox1->TabIndex = 1;
this->richTextBox1->Text = L"";
//
// richTextBox2
//
this->richTextBox2->Location = System::Drawing::Point(13, 138);
this->richTextBox2->Name = L"richTextBox2";
this->richTextBox2->Size = System::Drawing::Size(259, 96);
this->richTextBox2->TabIndex = 2;
this->richTextBox2->Text = L"";
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(13, 3);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(36, 16);
this->label1->TabIndex = 3;
this->label1->Text = L"Input";
this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label2->Location = System::Drawing::Point(10, 121);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(98, 16);
this->label2->TabIndex = 4;
this->label2->Text = L"Encrypted Text";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 284);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->richTextBox2);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->Encrypt);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
*private: System::Void Encrypt_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Text = "";
for (int a = 0; a < textBox1->Text->Length; a++)
{
richTextBox1->Text += Convert::ToChar(Encrypt(textBox1->Text[a]));
}
}*
};
}
I am using Visual Studio 2008 Express. When I try and use a newer version of visual studio windows forms are based on C# and not C++ and I can't even get it to build as far as in 2008. The main errors that are thrown are:
1> Inline native assembly not supported in managed code
error C3862: 'Encrypt': cannot compile an unmanaged function with /clr:pure or /clr:safe
C2227: left of '->Text' must point to class/struct/union/generic type
1> type is ''unknown-type''
Do I maybe need to put the char Encrypt function within the .ccp source file that belongs to the project? Any help would be greatly appreciated.
I'm trying to create an event aggregator in C++/CLI, I know that the valid syntax in C# would be as follows:
//C# code
public partial class Foo : UserControl, IView, IDisposable
{
private IEventAggregator _aggregator;
public Foo(IEventAggregator aggregator)
{
InitializeComponent();
this._aggregator = aggregator;
if (this._aggregator == null)
throw new Exception("null pointer");
_subToken =_aggregator.GetEvent<fooEvent>().Subscribe(Handler, ThreadOption.UIThread, false);
}
private SubscriptionToken _subToken = null;
private void Handler(fooEventPayload args)
{
//this gets run on the event
}
}
However directly converting this to C++/CLI gives the error "a pointer-to-member is not valid for a managed class" on the indicated line. Is there a workaround? I think it has something to do with how C# generates "Action".
//C++/CLI code
ref class Foo
{
public:
Foo(IEventAggregator^ aggregator)
{
void InitializeComponent();
this->_aggregator = aggregator;
if (this->_aggregator == nullptr)
throw gcnew Exception("null pointer");
//error in the following line on Hander, a pointer-to-member is not valid for a managed class
_subToken = _aggregator->GetEvent<fooEvent^>()->Subscribe(Handler, ThreadOption::UIThread, false);
private:
IEventAggregator ^ _aggregator;
SubscriptionToken ^ _addActorPipelineToken = nullptr;
void Handler(fooEventPayload^ args)
{
//this gets run on the event
}
}
You need to explicitly instantiate the delegate object, rather than allowing C# to do this for you.
_subToken = _aggregator->GetEvent<fooEvent^>()->Subscribe(
gcnew Action<fooEventPayload^>(this, &Foo::Handler), ThreadOption::UIThread, false);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Explicitly instantiate the delegate.
// ^^^^ Object to call the delegate on.
// ^^^^^^^^^^^^^ C++-style reference to the method.
High, guys! Sorry to ask you this question, but I canĀ“t find what causes the error. Also, I'm new at Visual C++/CLI, so I know my code could use some polishing.
In short, what I'm trying to do is capture data from a form to build a class.
I'd appreciate any help. THANKS IN ADVANCE.
My code:
// Form4A.h
#pragma once
# include "Tutors.h"
namespace SisPro
{
//.... more code
public ref class Form4A : public System::Windows::Forms::Form
{
#pragma region Windows Form Designer generated code
//.... more code
#pragma endregion
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^ m1 = comboBox14->SelectedItem->ToString();
String ^ m2 = comboBox19->SelectedItem->ToString();
String ^ m3 = comboBox20->SelectedItem->ToString();
Tutors.add_tutor(m1, m2, m3);// ERROR C2143
}
};
}
SOMWHERE ELSE:
//Tutors.h
using namespace System;
public ref class Tutors
{
public:
Tutors();
void add_tutor(String ^ m1, String ^ m2, String ^ m3)
private:
String ^ ID;
String ^ LASTNAME;
String ^ NAME;
};
// tutors.cpp
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "Tutores.h"
Tutors::Tutors()
{
ID = "";
LASTNAME = "";
NAME = "";
}
void Tutors::add_tutor(String ^ m1, String ^ m2, String ^ m3)
{
ID = m1;
LASTNAME = m2;
NAME = m3;
return;
}
Add field Tutors in class Form4A. And add field initialization in constructor.
public ref class Form4A : public System::Windows::Forms::Form
{
#pragma region Windows Form Designer generated code
//.... more code
Form4A()
{
//..
this->Tutors = gcnew Tutors();
}
#pragma endregion
Tutors^ Tutors;
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^ m1 = comboBox14->SelectedItem->ToString();
String ^ m2 = comboBox19->SelectedItem->ToString();
String ^ m3 = comboBox20->SelectedItem->ToString();
Tutors.add_tutor(m1, m2, m3);// ERROR C2143
}
};
I read this question: Command Line Parser for .NET.
I thought that was what I was looking for, but the library Command Line Parser Library is not Compact framework friendly...
I REALLY don't want to write a CL parser and I have been drifting away from the real purpose of my little app because of this unfortunate trial.
Does someone know of a library that fits the compact-framework? (preferably with simplicity and functionality like the one mentioned above)
Does not matter whether version 2 or 3.5
I developed this framework, maybe it helps:
The SysCommand is a powerful cross-platform framework, to develop Console Applications in .NET. Is simple, type-safe, and with great influences of the MVC pattern.
https://github.com/juniorgasparotto/SysCommand
namespace Example.Initialization.Simple
{
using SysCommand.ConsoleApp;
public class Program
{
public static int Main(string[] args)
{
return App.RunApplication();
}
}
// Classes inheriting from `Command` will be automatically found by the system
// and its public properties and methods will be available for use.
public class MyCommand : Command
{
public void Main(string arg1, int? arg2 = null)
{
if (arg1 != null)
this.App.Console.Write(string.Format("Main arg1='{0}'", arg1));
if (arg2 != null)
this.App.Console.Write(string.Format("Main arg2='{0}'", arg2));
}
public void MyAction(bool a)
{
this.App.Console.Write(string.Format("MyAction a='{0}'", a));
}
}
}
Tests:
// auto-generate help
$ my-app.exe help
// method "Main" typed
$ my-app.exe --arg1 value --arg2 1000
// or without "--arg2"
$ my-app.exe --arg1 value
// actions support
$ my-app.exe my-action -a
This is what I'm using. I borrowed it from somewhere, but not sure where:
using System.Collections.Specialized;
using System.Text.RegularExpressions;
/// <summary>
/// Parses the command line arguments into a name/value collection
/// </summary>
public class CommandLineArgumentParser
{
#region Fields
private StringDictionary parameters;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CommandLineArgumentParser"/> class.
/// </summary>
/// <param name="args">command-line arguments
/// </param>
public CommandLineArgumentParser(string[] args)
{
this.parameters = new StringDictionary();
Regex spliter = new Regex(#"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Regex remover = new Regex(#"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
string parameter = null;
string[] parts;
// Valid parameters forms:
// {-,/,--}param{ ,=,:}((",')value(",'))
// Examples:
// -param1 value1 --param2 /param3:"Test-:-work"
// /param4=happy -param5 '--=nice=--'
foreach (string txt in args)
{
// Look for new parameters (-,/ or --) and a
// possible enclosed value (=,:)
parts = spliter.Split(txt, 3);
switch (parts.Length)
{
// Found a value (for the last parameter
// found (space separator))
case 1:
if (parameter != null)
{
if (!this.parameters.ContainsKey(parameter))
{
parts[0] = remover.Replace(parts[0], "$1");
this.parameters.Add(parameter, parts[0]);
}
parameter = null;
}
// else Error: no parameter waiting for a value (skipped)
break;
// Found just a parameter
case 2:
// The last parameter is still waiting.
// With no value, set it to true.
if (parameter != null)
{
if (!this.parameters.ContainsKey(parameter))
{
this.parameters.Add(parameter, "true");
}
}
parameter = parts[1];
break;
// Parameter with enclosed value
case 3:
// The last parameter is still waiting.
// With no value, set it to true.
if (parameter != null)
{
if (!this.parameters.ContainsKey(parameter))
{
this.parameters.Add(parameter, "true");
}
}
parameter = parts[1];
// Remove possible enclosing characters (",')
if (!this.parameters.ContainsKey(parameter))
{
parts[2] = remover.Replace(parts[2], "$1");
this.parameters.Add(parameter, parts[2]);
}
parameter = null;
break;
}
}
// In case a parameter is still waiting
if (parameter != null)
{
if (!this.parameters.ContainsKey(parameter))
{
this.parameters.Add(parameter, "true");
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets a count of command line arguments
/// </summary>
public int Count
{
get
{
return this.parameters.Count;
}
}
/// <summary>
/// Gets the value with the given parameter name
/// </summary>
/// <param name="param">name of the parameter</param>
/// <returns>the value of the parameter</returns>
public string this[string param]
{
get
{
return this.parameters[param];
}
}
#endregion
}
http://commandline.codeplex.com/ I've used this so many times I've lost count. Maybe it works for CE. If not, it'll provide a fantastic starting point.