c++/cli DLL openFileDialog freeze and block the Test aplication - dll

I have a strange problem ...
I have implement the following fonction
private:
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->Filter = (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
{
// her is my listview code
myStream->Close();
}
}
}
in my DLL:
my problem is that, when testing the code as dll, the dialog is not displayed but freezes the application Completely and may be terminated only by Task Manager ...no idea what was going on there ... gives no sense to me. who can help me please?

In the .NET Framework version 2.0, you can also specify the COM threading model for a C++ application using the /CLRTHREADATTRIBUTE (Set CLR Thread Attribute) linker option.
VS Setting:STA threading attribute (/CLRTHREADATTRIBUTE:STA)
https://msdn.microsoft.com/en-us//library/system.stathreadattribute%28v=vs.80%29.aspx

openFileDialog1->ShowHelp = true;
I put this line then it solved the problem.

Related

Using Camera by C++/WinRT

I want to use Camera(CameraCaptureUI or MediaCapture class) in C++/WinRT.
Microsoft document sample code is written by C# and JavaScript.
https://learn.microsoft.com/en-us/uwp/api/Windows.Media.Capture.CameraCaptureUI#code-snippet-1
MFC + C++/WinRT
void CWinRTtestDlg::OnBnClickedButtonToast()
{
// show toast
auto notificationManager = ToastNotificationManager::GetDefault();
auto toastXml = ToastNotificationManager::GetTemplateContent(ToastTemplateType::ToastText01);
auto textNode = toastXml.GetElementsByTagName(L"text").Item(0);
textNode.AppendChild(toastXml.CreateTextNode(L"Hello C++/WinRT!"));
auto toast = ToastNotification(toastXml);
toast.ExpirationTime(winrt::clock::now() + std::chrono::hours() * 2);
notificationManager.CreateToastNotifier().Show(toast);
}
this code is working.
IAsyncAction Camera()
{
auto cameraManager = CameraCaptureUI();
cameraManager.PhotoSettings().CroppedAspectRatio(Size(4, 3));
cameraManager.PhotoSettings().Format(CameraCaptureUIPhotoFormat::Jpeg);
auto file{ co_await cameraManager.CaptureFileAsync(CameraCaptureUIMode::Photo) };
}
void CWinRTtestDlg::OnBnClickedButtonCamera()
{
winrt::init_apartment();
auto image = Camera();
}
but this code is NOT working...
C++ project setting
Additional Option /await
C++ Language Standard C++17
Conformance mode No
Teams are slowly adding C++ samples, but it's going to take time. For the most part, a transliteration shouldn't be too hard. If it's a C# constructor, replace it with a C++ constructor. For example:
Widget widget = new Widget(123);
Becomes:
Widget widget(123);
Properties don't exist in C++ so you can just add parentheses and treat it as a method. You can also use coroutines, so an await in C# becomes a co_await in C++.

Rename "params" parameter in Java Bindings Library with Mono for Android?

I'm trying to create a Java Binding Library for BugSense, but one of the methods has a parameter named "params" which is a reserved word in C#. I've tried to use the Metadata.xml file to rename it, but I can't figure out how to access the class, let alone the method or it's parameter.
Here is the problem code it's generating:
namespace Com.Bugsense.Trace {
[global::Android.Runtime.Register ("com/bugsense/trace/ActivityAsyncTask", DoNotGenerateAcw=true)]
internal partial class ActivityAsyncTaskInvoker : ActivityAsyncTask {
static IntPtr id_doInBackground_arrayLjava_lang_Object_;
[Register ("doInBackground", "([Ljava/lang/Object;)Ljava/lang/Object;", "GetDoInBackground_arrayLjava_lang_Object_Handler")]
protected override global::Java.Lang.Object DoInBackground (global::Java.Lang.Object[] params)
{
if (id_doInBackground_arrayLjava_lang_Object_ == IntPtr.Zero)
id_doInBackground_arrayLjava_lang_Object_ = JNIEnv.GetMethodID (class_ref, "doInBackground", "([Ljava/lang/Object;)Ljava/lang/Object;");
IntPtr native_params = JNIEnv.NewArray (params);
global::Java.Lang.Object __ret = Java.Lang.Object.GetObject<global::Java.Lang.Object> (JNIEnv.CallObjectMethod (Handle, id_doInBackground_arrayLjava_lang_Object_, new JValue (native_params)), JniHandleOwnership.TransferLocalRef);
if (params != null) {
JNIEnv.CopyArray (native_params, params);
JNIEnv.DeleteLocalRef (native_params);
}
return __ret;
}
}
}
Here is my mapping, which I feel should work, but just refuses to.
<attr path="/api/package[#name='com.bugsense.trace']/class[#name='ActivityAsyncTaskInvoker']/method[#name='doInBackground']/parameter[#name='params']" name="managedName">#params</attr>
I've tried everything I can think of. Please, HELP!
So, turns out it's just a bug in the current version of Mono for Android. If you update to the 4.2.4 build, which is in beta, everything compiles fine.

Managed C++, Object reference not set to an instance of an object

I've run into this problem before, but never in a situation like this. I'm completely confused. As the question states, I'm getting the runtime error "Object reference not set to an instance of an object." Using the debugger tools, I think I've pinpointed the problem to this line:
dataFileLocation = path;
The entire function is here:
void DATReader::SetPath(String^ path)
{
if(!File::Exists(path))
{
MessageBox::Show( "DATReader (missing dat file: \n"+path+"\n )", "Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
dataFileLocation = path;
}
dataFileLocation is declared here, but nothing is assigned to it:
ref class DATReader
{
private:
System::String^ dataFileLocation;
// ...
}
Now I know the reason I'm getting the error is because dataFileLocation is assigned to nothing. But I'm having problems assigning it. When I add = 0; to it, it won't build because its a ref class. When I try to assigned it to = 0; in the constructor, it yells at me for trying to convert it from a System::String^ to an int. If I assign it to a = gcnew String(""); it builds, but throws the same runtime exception.
I don't get it, am I reading the debugger wrong, and this isn't the source of the problem at all? I've just started to use managed code recently, so I'm confused :\
You may want to check and make sure your DATReader object isn't null as well It may be throwing the exception at your DATReader.SetPath() call.
This is a nicety in C# that's missing in C++/CLI. C# generates code that ensures this can never be null. Easily seen in the debugger by setting a breakpoint on the method and inspecting "this". Here's an example program that reproduces the exception:
#include "stdafx.h"
using namespace System;
ref class Example {
String^ dataFileLocation;
public:
void SetPath(String^ path) {
dataFileLocation = path; // Set breakpoint here and inspect "this"
}
};
int main(array<System::String ^> ^args)
{
Example^ obj /* = gcnew Example */;
obj->SetPath("foo");
return 0;
}
Remove the /* */ comments to fix. Fix your code by looking at the call stack to find the method that forgot to instantiate the object.
Managed C++ uses nullptr for null references. So you can check:
if (path == nullptr) { ... }
or use:
if (!String::IsNullOrEmpty(path))

Run task in Visual Basic?

I am writing a small app in VB and I would like to know how I would set it up so that when a user pressed a button, a sechduled task is ran. Keep in mind that this task is already created, I just need it to run.
Any ideas?
Thanks
Use the System.Diagnostics.Process class. You can create a process and run it with Process.Start() method.
EDIT:
Following code sample starts the helloworld.exe. This is just to give an idea about the Process class. You can find this example in Process.Start Method
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
public static void Main()
{
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// This code assumes the process you are starting will terminate itself.
// Given that is is started without a window so you cannot terminate it
// on the desktop, it must terminate itself or you can do it programmatically
// from this application using the Kill method.
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
How about using the recently added System.Threading.Tasks library?
Link: http://msdn.microsoft.com/en-us/library/system.threading.tasks.aspx

SetCompatibleTextRenderingDefault in .NET Class Library containing a form

I have a .net class library with a com class that calls a form.
I want to to SetCompatibleTextRenderingDefault(false) to ensure the form fonts look nice.
If I run the command in the class constructor I get the following error:
SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.
Where can/should I run this? Surely there is no earlier place than sub New!
Thank in advance
Jon
Edit1: To clarify, I get this error when initiating the class from a .net test harness, if I call it from a VB6 app then I simply get "Automation Error"
Edit2: Is the answer that I cannot use SetCompatibleTextRenderingDefault in a com class when calling from a vb6 app?? Maybe it's the "parent" app that needs to call this method and as such a vb6 app cannot?
Edit3: Maybe I am asking this question in the wrong way! - Maybe the question is: how can I make the fonts look nice in a .net class library form called from a vb6 app?
A possible workaround would be to set the property manually on all buttons and labels in the form constructor:
public Form1()
{
InitializeComponent();
DisableCompatibleTextRendering(this);
}
private static void DisableCompatibleTextRendering(Control c)
{
var button = (c as ButtonBase);
var label = (c as Label);
if (button != null)
{
button.UseCompatibleTextRendering = false;
}
if (label != null)
{
label.UseCompatibleTextRendering = false;
}
foreach (var child in c.Controls.Cast<Control>())
{
DisableCompatibleTextRendering(child);
}
}
Place this inside the application startup code before the first window is created. Under C# this would be the main routine that then creates the initial window.