Xamarin.Mac NSOpenPanel and NSSavePanel In Sandbox - monomac

I have a Xamarin.Mac application in Sandbox mode that need to allow a user to choose any file from there system since my application is a Viewer.
I use NSOpenPanel and NSSavePanel but my application crash without any crash logs when running the code.
I have added com.apple.security.files.user-selected.read-write to the Entitlements file etc.
Code:
var panel = NSOpenPanel.OpenPanel;
panel.FloatingPanel = true;
panel.CanChooseDirectories = true;
panel.CanChooseFiles = true;
int i = panel.RunModal ();
if (i == 1 && panel.Urls != null) {
foreach (NSUrl url in panel.Urls) {
}
}

Seam to be a bug in Xamarin.Mac:
https://bugzilla.xamarin.com/show_bug.cgi?id=24466

Related

Using a mutex to limit instance of application is not respected if one instance if started from Visual Studio

In InitInstance of my dialog application I use this code to detect other running versions:
strOwner.LoadString(IDS_APP_MUTEX);
m_hMutex = ::CreateMutex(nullptr, FALSE, strOwner);
HWND hOtherInstance = nullptr;
if (DetectRunningInstance(hOtherInstance))
{
DetectFileToOpenFromFileExplorer();
TryToOpenFileInOtherInstance(hOtherInstance);
return FALSE; // Terminates the creation
}
The other functions referred to are:
bool CMeetingScheduleAssistantApp::DetectRunningInstance(HWND& rhOtherWnd) noexcept
{
HWND hOther = nullptr;
if (m_hMutex != nullptr) // indicates running instance
{
if (::GetLastError() == ERROR_ALREADY_EXISTS)
{
EnumWindows(searcher, reinterpret_cast<LPARAM>(&hOther));
if (hOther != nullptr)
{
::SetForegroundWindow(::GetLastActivePopup(hOther));
if (IsIconic(hOther))
{
::ShowWindow(hOther, SW_RESTORE);
}
rhOtherWnd = hOther;
return true; // terminates the creation
}
}
}
return false;
}
And:
void CMeetingScheduleAssistantApp::DetectFileToOpenFromFileExplorer()
{
CCommandLineInfo cmdInfo;
m_bOpenFileFromFileExplorer = false;
m_strFileToOpenFromFileExplorerPath;
ParseCommandLine(cmdInfo);
if (PathFileExists(cmdInfo.m_strFileName))
{
m_bOpenFileFromFileExplorer = true;
m_strFileToOpenFromFileExplorerPath = cmdInfo.m_strFileName;
}
}
And:
void CMeetingScheduleAssistantApp::TryToOpenFileInOtherInstance(HWND hOtherInstance)
{
CString strFile = GetFileToOpenFromFileExplorerPath();
S_COPY_PACKET sCopyDataPacket{};
_tcscpy_s(sCopyDataPacket.szFile, strFile);
sCopyDataPacket.guidSignature = CopyData_Signature;
COPYDATASTRUCT cds;
cds.dwData = COPYDATA_TYPE_MSA;
cds.cbData = sizeof(sCopyDataPacket);
cds.lpData = &sCopyDataPacket;
DWORD_PTR dwResult{};
if (SendMessageTimeout(hOtherInstance, WM_COPYDATA,
NULL, reinterpret_cast<LPARAM>(&cds), SMTO_BLOCK, 2000, &dwResult) != 0)
{
// The message was sent and processed
if (dwResult == FALSE)
{
// The other instance returned FALSE. This is probably because it
// has a pop-up window open so can't open the file
::OutputDebugString(_T("InitInstance::SendMessageTimeout [dwResult was FALSE].\n"));
}
}
else
{
const DWORD dwError = ::GetLastError();
if (dwError == ERROR_TIMEOUT)
{
// The message timed out for some reason
::OutputDebugString(_T("InitInstance::SendMessageTimeout [ERROR_TIMEOUT].\n"));
}
else
{
// Another unknown error
}
CString strError;
strError.Format(_T("InitInstance::SendMessageTimeout [%d: %s]\n"), dwError,
(LPCTSTR)GetLastErrorAsStringEx(dwError));
::OutputDebugString(strError);
}
}
And the seracher:
BOOL CALLBACK CMeetingScheduleAssistantApp::searcher(HWND hWnd, LPARAM lParam) noexcept
{
DWORD_PTR result{};
const LRESULT ok = ::SendMessageTimeout(hWnd,
theApp.UWM_ARE_YOU_ME_MSG,
0, 0,
SMTO_BLOCK |
SMTO_ABORTIFHUNG,
200,
&result);
if (ok == 0)
return TRUE; // ignore this and continue
if (result == theApp.UWM_ARE_YOU_ME_MSG)
{
// found it
HWND *target = reinterpret_cast<HWND *>(lParam);
*target = hWnd;
return FALSE; // stop search
}
return TRUE; // continue search
}
Under normal circumstances this is fully functional. But I have a specific scenario where my mutex is not honored. I think this started with Visual Studio 2022 but I no longer have 2019 edition to confirm.
The scenario
My PC has an instance of the software I am developing "installed" and it has a shortcut on the taskbar. This links to the executable in the installed folder. If I simply use this shortcut then no issues, no multiple instances.
But, if I start my application from inside VS2022 instead, and then accidently click my other shortcut on the taskbar, I end up with two instances running. Why does this happen? I appreciate the otherinstance is running inside VS2022 but it is still the same software.
Is this by design or a coding oversight?
Update
If I manually double-click the release build EXE in file explorer, and then try to invoke the application in my VS2022 environment, it will jump to the existing instance. But ...
If I invoke the application in my VS2022 environment first, and then double-click the same release build EXE in file explorer, I end up with a multiple instance.

how to check outlook is configured for current user

I want to check if outlook is installed on given machine and configured for current user. I am using below code...
private static bool IsOutlookProfileConfigured2()
{
try
{
RegistryKey currentUser = Registry.CurrentUser;
RegistryKey regWMSprofile = currentUser.OpenSubKey
(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles");
if (regWMSprofile == null)
{
regWMSprofile = currentUser.OpenSubKey
(#"Software\Microsoft\Office\15.0\Outlook\Profiles");
if (regWMSprofile == null)
{
return false;
}
Console.WriteLine(#"Found on path==>Software\Microsoft\Office\15.0\Outlook\Profiles");
}
else
{
Console.WriteLine(#"Found on path==>SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles");
}
Outlook.Application OlApplication = new Outlook.Application();
if (regWMSprofile.SubKeyCount > 0)
{
if (OlApplication == null)
{
return false;
}
Outlook.NameSpace olNameSpace =
OlApplication.GetNamespace("MAPI");
if (olNameSpace != null && olNameSpace.Accounts !=
null && olNameSpace.Accounts.Count > 0)
{
return true;
}
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
return false;
}
method is saying outlook profile is found at "Software\Microsoft\Office\15.0\Outlook\Profiles".
Outlook 2013 is installed on users machine but not configured.
so this method opens outlook profile configuration wizard.
Can anyone help me with fixing this function or some sample code that will check if outlook is configured for current user and should not start profile configuration wizard.
Thanks in advance.
If you find any folder inside this 'Profiles' folder, then the Outlook
is configured for the current user.
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows
Messaging Subsystem\Profiles
Check link:- How to detect whether Outlook is configured on machine using Regis

::CoResumeClassObect() fails on Windows Server 2012 R2 with HRESULT : 0x800706c6(The array bounds are invalid)

I have a COM Server , and I want to use it as a service. My application has a installer component which creates a user (CN_Service) in local system and install this COM server as a service using below command:
Connect.exe /service /noreinstall /user:CN_Service /password:somePassword /depends:lanmanworkstation
I have another component, which is responsible for starting this installed service. In its winmain(), I am using below code to register the COM server class objects (in SUSPENDED mode), it successfully registers the objects and returns HRESULT as S_OK which is as expected.
int CAppModule::WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR /* lpCmdLine */, int nShowCmd)
{
::AfxSetResourceHandle(hInstance);
_strServiceName.LoadString(IDS_SRVL_SERVICENAME);
_strDisplayName.LoadString(IDS_SRVL_DISPLAYNAME);
VERIFY(gNTEventLog.SetSource(_strDisplayName));
// Copy parameters
m_hInstance = hInstance;
m_nCmdShow = nShowCmd;
_strCmdLine = ::GetCommandLine();
_qappmodel->InitCOMSupport(); //::CoInitializeEx(NULL, COINIT_MULTITHREADED);
// Process command line options
bool bContinue = true;
int nExitCode = 0;
if (ProcessCommandLine(_strCmdLine, &bContinue) && bContinue)
{
if (_patlmodule != NULL)
{
//_patlmodule is an interface pointer and private member of CAppModule
HRESULT hr = _patlmodule->RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
if (FAILED(hr))
{
ASSERT_NOT_REACHED();
gNTEventLog.LogHR(_T("Failed to register the class objects"), hr);
return 2;
}
}
nExitCode = InternalWinMain();
if (_patlmodule != NULL)
{
_patlmodule->RevokeClassObjects();
}
}
_qappmodel->UninitCOMSupport();
// When we get here, the service has been stopped
return nExitCode;
}
But when I try to resume the registered class objects using ::CoResumeClassObjects(), it throws an error ‘Failed to resume class objects’ with HRESULT value:
800706c6 (The array bounds are invalid)
I am using below code to resume the class objects:
BOOL CAppModule::InitInstance()
{
// Increment usage count
if (_patlmodule != NULL)
{
_patlmodule->Lock();
HRESULT hr = ::CoResumeClassObjects(); //Fails here
if (FAILED(hr))
{
ASSERT_NOT_REACHED();
if (hr == CO_E_WRONG_SERVER_IDENTITY)
{
gNTEventLog.LogHR(_T("Failed to resume class objects (registered as a service?)"), hr);
}
else
{
gNTEventLog.LogHR(_T("Failed to resume class objects"), hr);
}
return FALSE;
}
}
return TRUE;
}
Problem Statement: The important thing is that, I want to run this code on Windows Server 2012 R2 machine, where the method ::CoResumeClassObjects() fails. This same piece of code runs fine on Windows Server 2008 machine.
Please suggest me whether server2012 box has anything to do with ::CoResumeClassObjects() method.
Also I am creating a separate local user(which is an app requirement) for registering the exe as service in code and responsible for starting/stopping service. Can It be a problem on win server 2012 ?
Any help would be appreciated…

Windows + SetCommState how to set RTS?

I have an application that has been ported up from VC++ 6 where it worked fine. The code in question uses the WinAPI for a serial device driver. When ported to VS2012, the same code behaves rather differently.
Create a DCB, set SetCommState and go. CTS was set high, RTS was set high and you were on your way.
Since ported up to VS2012 Pro MFC, I am finding that it sets up SetCommState the same with or without hardware flow control:
memset(&dcb, 0x00, sizeof(dcb));
dcb.DCBlength = sizeof(DCB);
// Must be TRUE, only binary mode in Windows
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
// XOn/XOff disabled
dcb.fTXContinueOnXoff = TRUE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.XonLim = 0;
dcb.XoffLim = 0;
dcb.XonChar = 0;
dcb.XoffChar = 0;
// Misc Stuff
dcb.EofChar = 0;
dcb.EvtChar = 0;
dcb.ErrorChar = 0;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fAbortOnError = FALSE;
// 8N1 Setup
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
// Baud Rate
if (dwBaudRate == BAUD_115200)
{
dcb.BaudRate = CBR_115200;
}
else
{
dcb.BaudRate = CBR_38400;
}
// setup hardware flow control
if (bHardware == eYesHardwareFlowControl)
{
// ================ FLOW CONTROL ON ================
switch (bIgnoreCTS)
{
case eIgnoreCTS:
dcb.fOutxCtsFlow = FALSE;
break;
case eEnableCTS:
dcb.fOutxCtsFlow = TRUE;
break;
default:
case eCTSDecideLater:
dcb.fOutxCtsFlow = TRUE;
break;
}
// DSR Flow Control
dcb.fDsrSensitivity = FALSE;
dcb.fOutxDsrFlow = FALSE;
// <<Hardware flow control On(TRUE) Off(FALSE)>>
dcb.fDtrControl = DTR_CONTROL_ENABLE;
// <<Hardware flow control On(_HANDSHAKE) Off(_ENBLE)>>
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
}
else
{
// ================ FLOW CONTROL OFF ================
switch (bIgnoreCTS)
{
case eIgnoreCTS:
dcb.fOutxCtsFlow = FALSE;
break;
case eEnableCTS:
dcb.fOutxCtsFlow = TRUE;
break;
default:
case eCTSDecideLater:
dcb.fOutxCtsFlow = FALSE;
break;
}
// DSR Flow Control
dcb.fDsrSensitivity = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
}
if (SetCommState(m_hIdComDev, &dcb) == WINDOWS_API_ZERO_IS_BAD)
{
dwLastError = GetLastError();
}
At this point, I have set up the DCB, cleared it. I don't read in the previous state is I don't want to trust any garbage of anyone that previously used the port. Then I set up every field with Baud, Flow Control and CTS Ignore being the only optional items.
So, what I've noticed is that I can create situation where the Device and the PC don't communicate. Now, mind you, they always did before and they always work with Hyperterminal, ProComm, TeraTerm and so on. What I can see is that when these comm programs start (and the old VC++ 6 App), when the device is created and set up, RTS is immediately set high.
Now, my App, once the DCB is set up, SetCommState called; RTS is always LOW. And when this happens, communications is toast.
I want to FORCE RTS to be high and thought I could do it like this:
if (EscapeCommFunction(m_hIdComDev, CLRRTS) == WINDOWS_API_ZERO_IS_BAD)
{
dwLastError = GetLastError();
}
if (EscapeCommFunction(m_hIdComDev, SETRTS) == WINDOWS_API_ZERO_IS_BAD)
{
dwLastError = GetLastError();
}
But this fails, it gives an error 87 (parameter error). And I cannot quite figure it out. Even if I just SETRTS high only, it fails.
Any ideas on how I can force Windows to set RTS high after I setup the comm parameters in the DCB?
Well it turned out to be a device problem and not a windows problem at all.

How to programatically get the list of installed programs

I am creating a program which first checks whether a particular program has been installed or not, if it's installed it continues to execute other code, if it's not installed then it installs the application and then proceeds to execute the other code.
How do i check programatically in VC++ that the application has been installed or not
I got a C# function that does something similar, it looks on both the 32 bit and the 64 bit entries in the registry.I'm assuming you got the right name of the program you are looking for all you need is to match it with key "DisplayName". I doubt you'd have problems making it C++...It would go something like this
string SoftwareKey = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
bool found = false;
RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey);
foreach (string skName in rk.GetSubKeyNames())
{
RegistryKey sk = rk.OpenSubKey(skName);
if (sk.GetValue("DisplayName") != null &&
sk.GetValue("DisplayName").ToString().Equals("WhateverProgramYouAreLookingFor"))
{
//whatever you need to do with it
found = true;
break;
}
}
if(!found)
{
SoftwareKey = #"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
foreach (string skName in rk.GetSubKeyNames())
{
RegistryKey sk = rk.OpenSubKey(skName);
if (sk.GetValue("DisplayName") != null &&
sk.GetValue("DisplayName").ToString().Equals("WhateverProgramYouAreLookingFor"))
{
//whatever you need to do with it
found = true;
break;
}
}
}

Resources