Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I used the code you posted together with the latest version of the library and it works fine for me. One thing you might consider is wrapping as C++/CLI instead of using P/Invoke but this is another topic.</p> <p>I am working under the assumption you are using Visual Studio 2010 (oh well one has to start somewhere :-) ).</p> <p>One thing that is clearly not ok is:</p> <p><strong>Native:</strong></p> <p>extern "C"__declspec(dllexport) void __stdcall convertGeocentricToMGRS(const double x, const double y, const double z, char*&amp; mgrsString, Precision::Enum&amp; precision)</p> <p><strong>and C#:</strong></p> <p>[DllImport("CoordinateConversionWrapper.dll")] private static extern void convertGeocentricToMGRS(double x, double y, double z, ref char[] mgrsString, Precision precision);</p> <p>Do:</p> <pre><code>extern "C"__declspec(dllexport) void __stdcall convertGeocentricToMGRS(const double x, const double y, const double z, char** mgrsString, Precision::Enum&amp; precision) { MSP::CCS::CoordinateSystemParameters geocentricParameters(MSP::CCS::CoordinateType::geocentric); MSP::CCS::CoordinateSystemParameters mgrsParameters(MSP::CCS::CoordinateType::militaryGridReferenceSystem); MSP::CCS::CoordinateConversionService ccs( "WGE", &amp;geocentricParameters, "WGE", &amp;mgrsParameters ); MSP::CCS::Accuracy sourceAccuracy; MSP::CCS::Accuracy targetAccuracy; MSP::CCS::CartesianCoordinates sourceCoordinates(MSP::CCS::CoordinateType::geocentric, x, y, z); MSP::CCS::MGRSorUSNGCoordinates targetCoordinates; ccs.convertSourceToTarget( &amp;sourceCoordinates, &amp;sourceAccuracy, targetCoordinates, targetAccuracy ); int nMGRSLen = strlen( targetCoordinates.MGRSString() ); ::CoTaskMemFree(*mgrsString); *mgrsString = (char *)::CoTaskMemAlloc(nMGRSLen + 1); strcpy( *mgrsString, targetCoordinates.MGRSString() ); precision = targetCoordinates.precision(); } </code></pre> <p>Note that the char is passed in as pointer to pointer, and that CoTaskMemFree/CoTaskMemAlloc/strcpy are used (include Objbase.h for CoTaskMemAlloc)</p> <p>And in C# code you can:</p> <pre><code> [DllImport("MSPGeotransTest.dll", CharSet= CharSet.Ansi))] public static extern void convertGeocentricToMGRS(double x, double y, double z, ref string mgrsString, ref PrecisionEnum precision); </code></pre> <p>where:</p> <pre><code> public enum PrecisionEnum : uint { degree = 0, tenMinute = 1, minute = 2, tenSecond = 3, second = 4, tenthOfSecond = 5, hundrethOfSecond = 6, thousandthOfSecond = 7, tenThousandthOfSecond = 8 } </code></pre> <p>Other possibilities probably exist to do this...</p> <p>Some other useful things:</p> <p>In order to be able to debug make sure that:</p> <p>In Tools > Options > Debugging > General, "Enable Just My Code" is unchecked.</p> <p>In Project > Properties > Debug tab, "Enable unmanaged code debugging" is checked.</p> <p>Place a breakpoint in the C# method and when the breakpoint is reached you can step into F11 and reach the C++ code...</p> <p>I compiled the C++ Dll by choosing "Use Multi-Byte Character Set" (Configuration Properties\General\Character Set)</p> <p>Also it would seem the CoordinateConversionService constructor throws a CoordinateConversionException if it is unable to load the configuration files (it searches them it would seem in a path that can be configured through an environment variable named MSPCCS_DATA and if the environment variable is not defined it looks for them in ../../data/ relative to the exe path).</p> <p>Probably in the C++ wrapper methods maybe you would like to catch any exceptions that can be thrown by the called methods and return an error code... I.e. other situations that trigger exceptions is due to invalid input coordinates and so on.</p> <p>As I said I have a working example so if you want I can send it to you...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload