Apple idfa in Unity via Plugin - c#

I borrowed this code from another stack overflow answer:
example.mm
const char* getIdfa()
{
if([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled])
{
NSString * idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
//return (const char*)[idfa UTF8String]; // ##### malloc error I uncomment this line #####
}
return nil;
}
iOSplugin.cs
#if UNITY_IOS
[DllImport("__Internal")]
public static extern string getIdfa();
#endif
Xcode build settings that can affect the issue:
Project format: xcode 3.1 compatible
Obj-C Automatic Reference Counting: Yes
If I uncomment this line:
return (const char*)[fdsa UTF8String];
then I get the following error:
(531,0x19e9ef000) malloc: *** error for object 0x1357d44e1: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
There are some references to a function called cocoaToMonoString which is supposed to fix this problem, but I can't find any examples or documentation about it.
How do I return a string from Xcode back into Unity?

Just a two cents contribution. Now you can obtain IDFA (or GAID for android) using Application.RequestAdvertisingIdentifierAsync:
Application.RequestAdvertisingIdentifierAsync(
(string advertisingId, bool trackingEnabled, string error) =>
Debug.Log("advertisingId " + advertisingId + " " + trackingEnabled + " " + error)
);
Also that method returns a boolean value indicating true in case of success or false if the current platform doesn't support advertising identifiers.

[idfa UTF8String]
This is a reference to the internal byte array inside the NSString.
It can't be passed back to Unity/C#.
Add this function:
static const char* MakeStringCopy(const char* string)
{
if (string == NULL)
return NULL;
char* res = (char*)malloc(strlen(string) + 1);
strcpy(res, string);
return res;
}
and return:
return MakeStringCopy([idfa UTF8String]);

Related

How to open the "Active Directory Users and Computers" object properties dialog from c#?

Is there a way to call this dialog from c#?
I traced the apis, but non of the calls seems to call the dialog. Dsuiext.dll sounds very promissing, but there I foud just a LDAP browser.
This Microsoft sample provides the expected result. You pass an ADS path as parameter and it calls the property window.
PropSheetHost.exe "LDAP://CN=user,DC=MyDomain,DC=MyTldDomain"
It is important that it is case sensitive, so "ldap://.." doesn't work. The code is definitely not designed to get called multiple times before terminating, so it is probably the best way to use the exe without changes like that:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"PropSheetHost.exe";
startInfo.Arguments = #"LDAP://CN=user,DC=MyDomain,DC=MyTldDomain";
Process.Start(startInfo);
I wrote a wrapper to call it directly from C# and corrected the error what I found. Since I haven't programmed C for nearly 30 years, I am grateful for any hint if the implementation is incorrect. All changes are explained and marked with //MW: .... This works in my code, but you can open only one windows at a time and need to close it before opening another window.
The entry point:
__declspec(dllexport) HRESULT __stdcall CallPropSheetHost(const char* ldapPath)
{
TCHAR szTemp[MAX_ADSPATH_CHARS];
LPWSTR pwszADsPath = NULL;
HRESULT hr = E_FAIL; // MW: move before "if" and preset
CoInitialize(NULL);
{
//MW: copy the parameter
_tcsncpy_s(szTemp, ARRAYSIZE(szTemp), ldapPath, MAX_ADSPATH_CHARS - 1);
}
DWORD dwChars = lstrlen(szTemp) + 1;
pwszADsPath = new WCHAR[dwChars];
if (pwszADsPath)
{
HINSTANCE hInstance = NULL;
HWND hwndConsole = GetConsoleWindow();
if (hwndConsole)
{
hInstance = (HINSTANCE)(LONG_PTR)GetWindowLongPtr(hwndConsole, GWLP_HINSTANCE);
}
CPropSheetHost* pHost = new CPropSheetHost(hInstance);
LocalToWideChar(pwszADsPath, dwChars, szTemp, dwChars);
// Hold a reference count for the CPropSheetHost object.
pHost->AddRef();
hr = pHost->SetObject(pwszADsPath);
if (FAILED(hr))
{
goto ExitMain;
}
//MW: My implmentation returns E_Fail when the registration fails
hr = pHost->Run();
if (FAILED(hr))
{
pHost->Release();
goto ExitMain;
}
//Release the CPropSheetHost object. Other components may still hold a
//reference to the object, so this cannot just be deleted here. Let
//the object delete itself when all references are released.
pHost->Release();
}
ExitMain:
if (pwszADsPath)
{
delete pwszADsPath;
return hr; //MW: return th HRESULT
}
CoUninitialize();
return hr; //MW: return th HRESULT
}
The original implementation doesn't unregister a class. Therefore it fails when it's used multiple times. These are my changes in PropSheetHost.cpp to fix that.
//MW: new method
void CPropSheetHost::_UnregisterWndClass()
{
UnregisterClass(m_szHiddenWindowClass, m_hInst);
}
//MW: added a HRESULT and calling of "_UnregisterWndClass"
HRESULT CPropSheetHost::Run()
{
if (!m_spADObject.p)
{
return E_FAIL; //MW: added a return value
}
// Create the hidden window.
m_hwndHidden = _CreateHiddenWindow();
if (!m_hwndHidden)
{
return E_FAIL; //MW: added a return value
}
/*
Display the proeprty sheet. This is a modal call and will not return
until the property sheet is dimissed.
*/
_CreatePropertySheet();
// Destroy the hidden window.
DestroyWindow(m_hwndHidden);
//WM: Unregister the class; this call was missing
_UnregisterWndClass();
return ERROR_SUCCESS; //MW: added a return value
}
... and the call from C#:
using System;
using System.Runtime.InteropServices;
using System.Windows;
const int MAX_ADSPATH_CHARS = 2047;
[DllImport("PropSheetHost.dll", EntryPoint = "CallPropSheetHost", CallingConvention = CallingConvention.Cdecl)]
private static extern int CallPropSheetHost(string ldapPath);
///CAUTION:
/// * This call is modal and won't return until the called window is closed
/// * You can open only one window at a time. Trying opening a second window before closing the the first one fails
public static int Win32PropSheetHost(string distinguishedName, string serverOrDomain = null)
{
if (string.IsNullOrEmpty(distinguishedName)) throw new ArgumentNullException("EXC262: the distinguished name must not be null nor empty");
//<----------
/// Caution: "LDAP" must be uppercase!
string ldapPath = string.IsNullOrEmpty(serverOrDomain)
? $"LDAP://{ distinguishedName }"
: $"LDAP://{ serverOrDomain }/{ distinguishedName }";
if (ldapPath.Length > MAX_ADSPATH_CHARS) throw new ArgumentException($"EXC263: the complete lds path must not be longer than { MAX_ADSPATH_CHARS } characters (current path: \"{ ldapPath }\")");
//<----------
try
{
return CallPropSheetHost(ldapPath);
}
catch (DllNotFoundException ex)
{
/// Could't find a dll, mos likely our propsheethost.dll
return ResultWin32.ERROR_DLL_NOT_FOUND;
}
}
For the translation of the Windows Error Codes I use this class.

0xC0000005: Access violation reading location 0xffffffffffffffff

I have:
An unmanaged C++ app
A C++/CLI Wrapper
A C# GUI
I am seeing this crash occur only in Release and not debug. The crash also does not occur on neither debug or release when the unmanaged C++ app is run by itself.
All I can say is the crash occurs in this line of code:
if ((std::find(vstrEEDRRMachines.begin(), vstrEEDRRMachines.end(), m_sFrame.strSourceAddress) != vstrEEDRRMachines.end()
&& std::find(vstrEEDRRMachines.begin(), vstrEEDRRMachines.end(), m_sFrame.strDestAddress) != vstrEEDRRMachines.end())
|| (std::find(vstrRRRHMachines.begin(), vstrRRRHMachines.end(), m_sFrame.strSourceAddress) != vstrRRRHMachines.end()
&& std::find(vstrRRRHMachines.begin(), vstrRRRHMachines.end(), m_sFrame.strDestAddress) != vstrRRRHMachines.end()))
{
// Create appropriate buffer size for raw message (i.e. size of payload along with the extra padding
// for decoding)
m_sFrame.iMessageSize = m_sFrame.iPayloadLength;
m_sFrame.iOriginalMessageSize = m_sFrame.iPayloadLength;
m_sFrame.pszMessage = new unsigned char[m_sFrame.iPayloadLength + Constants::RSSL_DECODE_PADDING];
m_sFrame.pszOriginalMessage = new unsigned char[m_sFrame.iPayloadLength + Constants::RSSL_DECODE_PADDING];
}
Although I can't see what's inside vstrEEDRRMachines and vstrRRRHMachines (because we are in release), I can see when in the original C++/CLI wrapper that there are valid string entries in the vector.
The stack trace is as follows:
msvcr100.dll!0000000069abbdc0() [Frames below may be incorrect
and/or missing, no symbols loaded for msvcr100.dll]
DataVerifier.exe!std::_Find,std::allocator
__ptr64,std::basic_string,std::allocator
(std::basic_string,std::allocator > * _First, std::basic_string,std::allocator > *
_Last, const std::basic_string,std::allocator > &
_Val) Line 41 + 0x4a bytes C++
DataVerifier.exe!DataVerifier::CPCAPParser::Parse(const char * szFileName,
std::vector
& vSFramesRWF1Messages, std::vector,std::allocator
,std::allocator,std::allocator
vstrEEDRRMachines, std::vector,std::allocator
,std::allocator,std::allocator
vstrRRRHMachines, RsslDataDictionary & rsslDataDictionary) Line 178 + 0x19 bytes C++ [External Code]
DataVerifierLib.dll!DataVerifierLib::PCAPParserWrapper::ParseWrapper(System::String^
strInputFileNames) Line 136 + 0xf6 bytes C++
DataVerifierGUI.exe!DataVerifierGUI.Form1.button1_Click(object
sender, System.EventArgs e) Line 42 + 0x30 bytes C#
user32.dll!00007fff7a8c250d() user32.dll!00007fff7a8c2367()
System.Windows.Forms.ni.dll!00007fff535368c0()
The crash specifically occurs in c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm in this part of the code:
// TEMPLATE FUNCTION find
template<class _InIt,
class _Ty> inline
_InIt _Find(_InIt _First, _InIt _Last, const _Ty& _Val)
{ // find first matching _Val
for (; _First != _Last; ++_First)
if (*_First == _Val) <-- CRASH OCCURS HERE
break;
return (_First);
}
I have no idea what's going on, since release mode with C# + C++/CLI + C++ apps is where the crash is occurring. Is there any way I can easily fix this?
EDIT
It seems like this occurs 9 times out of 10, but i've noticed the times it does work, when I check the parameters of the Parse function just as it goes into the function (no lines are executed yet), all 3 vectors have normal values (the 1st one is 0, the second and third contain IP addresses as expected). However, most of the time, as soon as we get into the Parse function, all 3 vectors have negative size and capacities... and when they are used, everything goes boom. I'm going to past the entrance of the Parse function and also the C++/CLI wrapper before it.
bool CPCAPParser::Parse(const char* szFileName, std::vector<CRWFCapsule> &vSFramesRWF1Messages, std::vector<std::string> vstrEEDRRMachines, std::vector<std::string> vstrRRRHMachines, RsslDataDictionary &rsslDataDictionary) // change to a vector of rwf1capsules
{
This is the C++/CLI Wrapper:
// This is the main DLL file.
#include "stdafx.h"
#include "DataVerifierLib.h"
// Constructor Implementation
DataVerifierLib::PCAPParserWrapper::PCAPParserWrapper()
{
}
std::vector<std::string> DataVerifierLib::PCAPParserWrapper::CreateNewMachineCollection(std::vector<std::string> vstrNewMachines, std::vector<std::string> vstrMachine1, std::vector<std::string> vstrMachine2)
{
vstrNewMachines.reserve(vstrMachine1.size() + vstrMachine2.size()); // preallocate memory
vstrNewMachines.insert(vstrNewMachines.end(), vstrMachine1.begin(), vstrMachine1.end());
vstrNewMachines.insert(vstrNewMachines.end(), vstrMachine2.begin(), vstrMachine2.end());
return vstrNewMachines;
}
bool DataVerifierLib::PCAPParserWrapper::ParseWrapper(String^ managedString)
{
// String conversion from c# to c++
String^ managedStringTmp = managedString;
std::string strInputFileNames = msclr::interop::marshal_as<std::string>(managedStringTmp);
std::vector<std::string> vRRMachines;
std::vector<std::string> vRHMachines;
std::vector<std::string> vEEDMachines;
std::vector<std::string> vPorts; // decide on what checks are to be made. Should frame have matching src/dest ports? or just one of them.
std::vector<std::string> vEEDRRMachines;
std::vector<std::string> vRRRHMachines;
std::vector<std::string> vstrLines;
std::string strTxtFile = "ServerIPList.txt"; //argv[2]; // ServerIPList.txt
std::string strLine;
std::ifstream in(strTxtFile);
if (!in)
{
std::cout << "There was a problem opening the file." << std::endl;
std::cerr << "Error: " << strerror(errno) << std::endl;
return -1;
}
while (std::getline(in, strLine))
{
vstrLines.push_back(strLine);
}
for (int i = 0; i < vstrLines.size(); ++i)
{
if (vstrLines[i].substr(0, 2) == "rr")
{
boost::split(vRRMachines, vstrLines[i], boost::is_any_of(","));
if (vRRMachines.size() < 2)
return -1;
else
vRRMachines.erase(vRRMachines.begin());
}
else if (vstrLines[i].substr(0, 2) == "rh")
{
boost::split(vRHMachines, vstrLines[i], boost::is_any_of(","));
if (vRHMachines.size() < 2)
return -1;
else
vRHMachines.erase(vRHMachines.begin());
}
else if (vstrLines[i].substr(0, 3) == "eed")
{
boost::split(vEEDMachines, vstrLines[i], boost::is_any_of(","));
if (vEEDMachines.size() < 2)
return -1;
else
vEEDMachines.erase(vEEDMachines.begin());
}
else if (vstrLines[i].substr(0, 5) == "ports")
{
boost::split(vPorts, vstrLines[i], boost::is_any_of(","));
if (vPorts.size() < 2)
return -1;
else
vPorts.erase(vPorts.begin());
}
}
// Create a vector with EED/RR and RR/RH combined addresses
vEEDRRMachines = CreateNewMachineCollection(vEEDRRMachines, vEEDMachines, vRRMachines);
vRRRHMachines = CreateNewMachineCollection(vRRRHMachines, vRRMachines, vRHMachines);
// Initialise Rssl
RsslRet rsslRet;
RsslError rsslError;
rsslRet = rsslInitialize(RSSL_LOCK_NONE, &rsslError);
if (rsslRet != RSSL_RET_SUCCESS)
return -1;
// Initialise Field Dictionary
// To prevent memory issues, we need to use "malloc" for the data dictionary in order to load RWF.dat and enumtype.def
RsslDataDictionary *rsslDataDictionary = (RsslDataDictionary*)malloc(sizeof(RsslDataDictionary));
RsslBuffer rsslBufferError;
rsslClearDataDictionary(rsslDataDictionary);
if (rsslRet = rsslLoadFieldDictionary("RWF.DAT", rsslDataDictionary, &rsslBufferError) != RSSL_RET_SUCCESS)
{
// Ensure we free the data dictionary memory when finished
free(rsslDataDictionary);
//if (iDisplayType != DISPLAY)
// std::cout << "Could not load RDM Field Dictionary file." << std::endl;
return -1;
}
// Load enum dictionary
if (rsslLoadEnumTypeDictionary("enumtype.def", rsslDataDictionary, &rsslBufferError) != RSSL_RET_SUCCESS)
{
// Ensure we free the data dictionary memory when finished
free(rsslDataDictionary);
//if (iDisplayType != DISPLAY)
// std::cout << "Could not load Enum Type Dictionary file." << std::endl;
return -1;
}
std::string strCombinedFileName;
std::vector<CRWFCapsule> vRWFCapsules;
std::vector<std::string> vstrInputFileNames;
pPcapParser = new CPCAPParser(1,1); // Initiate C++ class instance
boost::algorithm::split_regex(vstrInputFileNames, strInputFileNames, boost::regex(","));
// Let's iterate through each PCAP file and parse it.
for (int i = 0; i < vstrInputFileNames.size(); ++i)
{
if (false == strCombinedFileName.empty())
{
strCombinedFileName.append("-");
}
if (false == pPcapParser->Parse(vstrInputFileNames[i].c_str(), vRWFCapsules, vEEDRRMachines, vRRRHMachines, *rsslDataDictionary))
{
delete pPcapParser;
// Ensure we free the data dictionary memory when finished
free(rsslDataDictionary);
vRWFCapsules.clear();
return -1;
}
strCombinedFileName = strCombinedFileName.append(pPcapParser->GetFileName());
}
//if (iDisplayType != NO_DISPLAY)
// std::cout << "Clearing up..." << std::endl;
delete pPcapParser;
if (rsslRet = rsslDeleteDataDictionary(rsslDataDictionary) != RSSL_RET_SUCCESS)
{
// Ensure we free the data dictionary memory when finished
free(rsslDataDictionary);
//if (iDisplayType != NO_DISPLAY)
// std::cout << "Problem deleting dictionary." << std::endl;
return -1;
}
// Ensure we free the data dictionary memory when finished
free(rsslDataDictionary);
if (vRWFCapsules.empty())
return -1;
CMessageSorter msgSorter(vEEDMachines, vRRMachines);
std::map<std::string, SMessageInfo> m_msMessageInfo;
std::map<std::string, SSystemInfo> m_msSystemInfo = msgSorter.SortMessages(vRWFCapsules, 1, m_msMessageInfo);
if (m_msSystemInfo.empty())
return -1;
CDataVerifier dataVerifier(strCombinedFileName, 1, 1);
dataVerifier.CreateOutputForAllMessages(m_msMessageInfo);
dataVerifier.Process(m_msSystemInfo);
// When clearing the vector, we have to make sure that the destructor of the RWF message is being called... so everything destroys itself correctly.
// Will need to check if destruction is down properly. However, how can we delete RWF Capsules encapsulated in eachother?...
vRWFCapsules.clear();
}
And its appropriate header file:
// DataVerifierLib.h
#pragma once
#include "../DataVerifier/CRWFCapsule.h"
#include "../DataVerifier/ISystem.h"
#include "../DataVerifier/CFrameAnalyser.h"
#include "../DataVerifier/CPCAPParser.h"
#include "../DataVerifier/CMessageSorter.h"
#include "../DataVerifier/CDataVerifier.h"
#include "../DataVerifier/CSourceDirectoryResponse.h"
#include "../DataVerifier/CDataVerifierConstants.h"
#include <vector>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <boost/algorithm/string.hpp>
#include <msclr/marshal_cppstd.h>
using namespace System;
using namespace DataVerifier;
namespace DataVerifierLib {
public ref class PCAPParserWrapper
{
// TODO: Add your methods for this class here.
public:
// Constructor
PCAPParserWrapper();
// Wrapper Methods
bool ParseWrapper(String^ strInputFileNames);
// Fill vectors with appropriate RR/EED/RH addresses
std::vector<std::string> CreateNewMachineCollection(std::vector<std::string> vstrNewMachines, std::vector<std::string> vstrMachine1, std::vector<std::string> vstrMachine2);
// Public Variable
double initVal;
private:
CPCAPParser *pPcapParser; // an instance of class in C++
};
}
Don't know why I have received down votes for this, as the solution could be quite useful to people mixing C# and a C++/CLI wrapper.
I realized that the suspect line was actually this line:
//String^ managedStringTmp = managedString;
//std::string strInputFileNames = msclr::interop::marshal_as<std::string>(managedStringTmp);
std::string strInputFileNames = marshalString<E_UTF8>(managedString);
I've obviously changed it so that it is using this new "marshalString" function. I used a marshal string template class from the link here, as it seems to address performance issues.
http://blog.nuclex-games.com/mono-dotnet/cxx-cli-string-marshaling/
Now all I need to do is figure out why returning true from the wrapper and assigning it to a boolean in C# returns false... but hopefully this will help some people out.
UPDATE
This explains why returning false or true in the wrapper back to C# won't work, and that we either need to use marshalling of the function (didn't want to do) or return 0 for false and 1 for true.
C# DllImport with C++ boolean function not returning correctly

custom implementation of iTunesMobileDevice.dll throws NullReferenceException

I had intended to implement the Manzana.dll library in order to detect iPhone connection events and interact with the device. The problem is that it only seems to work if the client machine has the iTunes dlls and resources installed, which I cannot rely on. Therefore I am trying to use a custom implementation of the Manzana source code to point it's references to the necessary iTunes files that I am including with the project.
Although everything looks ok the compiled library throws a NullReferenceException when used from my application. The application load and initializes ok, but when an iPhone is connected the connectedevent throws an exception.
The actual error is:
System.TypeInitializationException: The type initializer for 'istreamwrapper.MobileDevice' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at istreamwrapper.MobileDevice..cctor()
--- End of inner exception stack trace ---
at istreamwrapper.MobileDevice.AMDeviceNotificationSubscribe(DeviceNotificationCallback callback, UInt32 unused1, UInt32 unused2, UInt32 unused3, Void*& am_device_notification_ptr)
at istreamwrapper.iPhone.doConstruction()
I was able to use that to narrow down the problem to to this method from my iPhone class
private unsafe void doConstruction()
{
void* voidPtr;
this.dnc = new DeviceNotificationCallback(this.NotifyCallback);
this.drn1 = new DeviceRestoreNotificationCallback(this.DfuConnectCallback);
this.drn2 = new DeviceRestoreNotificationCallback(this.RecoveryConnectCallback);
this.drn3 = new DeviceRestoreNotificationCallback(this.DfuDisconnectCallback);
this.drn4 = new DeviceRestoreNotificationCallback(this.RecoveryDisconnectCallback);
int num = MobileDevice.AMDeviceNotificationSubscribe(this.dnc, 0, 0, 0, out voidPtr);
if (num != 0)
{
throw new Exception("AMDeviceNotificationSubscribe failed with error " + num);
}
num = MobileDevice.AMRestoreRegisterForDeviceNotifications(this.drn1, this.drn2, this.drn3, this.drn4, 0, null);
if (num != 0)
{
throw new Exception("AMRestoreRegisterForDeviceNotifications failed with error " + num);
}
this.current_directory = "/";
}
}
The issue comes from
num = MobileDevice.AMDeviceNotificationSubscribe(this.dnc, 0, 0, 0, out voidPtr);
which points to this code which is located in my MobileDevice class
[DllImport("iTunesMobileDevice.dll", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern int AMDeviceNotificationSubscribe(DeviceNotificationCallback callback, uint unused1, uint unused2, uint unused3, out void* am_device_notification_ptr);
That in turn seems to reference this in it's own class
namespace istreamwrapper
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void DeviceNotificationCallback(ref AMDeviceNotificationCallbackInfo callback_info);
}
which then points to another class with:
namespace istreamwrapper
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct AMDeviceNotificationCallbackInfo
{
internal unsafe void* dev_ptr;
public NotificationMessage msg;
public unsafe void* dev
{
get
{
return this.dev_ptr;
}
}
}
}
The vast majority of this code was copied straight from the Manzana.dll, the only thing I changed was where the itunesmobiledevice files are located (which is now a set path, rather detected at run time)
Old code:
namespace Manzana
{
internal class MobileDevice
{
private static readonly FileInfo iTunesMobileDeviceFile = new FileInfo(Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Apple Inc.\\Apple Mobile Device Support\\Shared", "iTunesMobileDeviceDLL", (object) "iTunesMobileDevice.dll").ToString());
private static readonly DirectoryInfo ApplicationSupportDirectory = new DirectoryInfo(Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Apple Inc.\\Apple Application Support", "InstallDir", (object) Environment.CurrentDirectory).ToString());
private const string DLLName = "iTunesMobileDevice.dll";
static MobileDevice()
{
string str = MobileDevice.iTunesMobileDeviceFile.DirectoryName;
if (!MobileDevice.iTunesMobileDeviceFile.Exists)
{
str = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + "\\Apple\\Mobile Device Support\\bin";
if (!File.Exists(str + "\\iTunesMobileDevice.dll"))
str = "C:\\Program Files\\Common Files\\Apple\\Mobile Device Support";
}
Environment.SetEnvironmentVariable("Path", string.Join(";", new string[3]
{
Environment.GetEnvironmentVariable("Path"),
str,
MobileDevice.ApplicationSupportDirectory.FullName
}));
}
New code:
namespace istreamwrapper
{
class MobileDevice
{
static MobileDevice()
{
string str = "[XX_MYPATHHERE_XX]\\Apple\\Mobile Device Support";
string AppSuppDirectory = #"[XX_MYPATHHERE_XX]\Apple\Apple Application Support";
Environment.SetEnvironmentVariable("Path", string.Join(";", new string[3] { Environment.GetEnvironmentVariable("Path"), str, AppSuppDirectory }));
}
Is there something I'm missing that is causing that call to return null? I'll admit I don't fully understand everything that is happening in the above code so it's entirely possible it's something simple.
Yes, I believe the answer was really that the path was incorrect and thus could not find the file. I just didn't realize this because the error it was throwing was too generic.

ExecuteInDefaultAppDomain returns 8013101B

I am trying to host CLR in my native Win32 C++ application.
CLR loading works okay, but when i try to execute a method in the assembly, then ExecuteInDefaultAppDomain returns 0x8013101B, and bails out.
Here is the code snippet:
// Managed Code
namespace ManagedLibrary
{
public class LibraryBootstrapper
{
static LibraryBootstrapper()
{
MessageBox.Show("Static LibraryBootsrapper");
}
public LibraryBootstrapper()
{
}
public static int Initialize(String str)
{
MessageBox.Show("Hi " + str + ", Library Bootsrapped");
return 0;
}
}
// Native Code
int tmain()
{
// Bind to the runtime.
ICLRRuntimeHost *pClrHost = NULL;
HRESULT hrCorBind = CorBindToRuntimeEx(
NULL, // Load the latest CLR version available
L"wks", // Workstation GC ("wks" or "svr" overrides)
0, // No flags needed
CLSID_CLRRuntimeHost,
IID_ICLRRuntimeHost,
(PVOID*)&pClrHost);
// Now, start the CLR.
HRESULT hrStart = pClrHost->Start();
DWORD result = 0;
// Load an assembly and execute a method in it.
HRESULT hrExecute = pClrHost->ExecuteInDefaultAppDomain(L"C:\\KIRAN\\Workspaces\\VS 2010\\HostCLR\\ManagedLibrary\\bin\\Debug\\ManagedLibrary.dll", L"ManagedLibrary.LibraryBootstrapper", L"Initialize", L"Kiran", &result);
//HRESULT hrStop = pClrHost->Stop();
return;
}
I figured it out!
The problem was that the versions of .NET frame that was being referenced by native and managed projects were different. Syncing that up worked.
And, btw, the error code 0x8013101B, corresponds to COR_E_NEWER_RUNTIME (see corerror.h), which helped me figure out the problem.
The error codes are explained here: http://blogs.msdn.com/b/yizhang/archive/2010/12/17/interpreting-hresults-returned-from-net-clr-0x8013xxxx.aspx

Debugging DLLImport in C# pt. 2

I am using the MySQL Embedded Library and using P/Invoke to call the necessary functions to start the server. We resolved some issues regarding it in this topic, however another issue has presented itself.
The mysql_server_init() function returns 0 if success, 1 if error. Unfortunately, in my code when it returns 1, and I use Marshal.GetLastWin32Error() the error code is 0. I am assuming that it is not picking up on the error being generated by mysql_server_init(), but I am at a loss as to how to find out where the problem is.
Here is the relevant code block...
[DllImportAttribute("libmysqld.dll", SetLastError = true)]
static extern int mysql_server_init(int argc, string[] argv, string[] groups);
static string[] server_options = new string[2];
static string[] server_groups = new string[3];
public static bool Start()
{
server_options[0] = "mysql_test"; // not used?
server_options[1] = "--defaults-file=./my.ini";
server_groups[0] = "client";
server_groups[1] = "server";
server_groups[2] = "\0";
if (mysql_server_init(2, server_options, server_groups) != 0)
{
int lastError = Marshal.GetLastWin32Error();
Console.WriteLine("MySQL Library Init Failed with error code: " + lastError);
return false;
}
Console.WriteLine("MySQL Library Started Successfully!");
return true;
}
The mysql_server_init function does not report errors via the Win32 error reporting mechanism SetLastError() and GetLastError(). So, you can"t use Marshal.GetLastWin32Error() to obtain the last error. The embedded mysql database reports errors via the functions mysql_error() and mysql_errno(). However, those functions seem to only report errors AFTER a successful call to mysql_server_init().
I think the problem of your code lies in the way you terminate your server_groups array.
You should use "null" instead of "\0" to "terminate" your array:
public static bool Start()
{
server_options[0] = "mysql_test"; // not used?
server_options[1] = "--defaults-file=./my.ini";
server_groups[0] = "client";
server_groups[1] = "server";
server_groups[2] = null;
if (mysql_server_init(2, server_options, server_groups) != 0)
{
int lastError = Marshal.GetLastWin32Error();
Console.WriteLine("MySQL Library Init Failed with error code: " + lastError);
return false;
}
}
Errors regarding your configuration should be printed to the console window by the mysql_server_init() function.
Hope, this helps.

Categories

Resources