Java.Nio.ByteBuffer to c# - c#

I am tring to use the following code in a c# xamarin project.
In Java,
ByteBuffer buffer=ByteBuffer.allocate(8);
UsbRequest request=new UsbRequest();
now I'm trying to convert these two line to c# for my xamarin project,
Java.Nio.ByteBuffer m_rinfo;
Int32 m_bytes = 64;
UsbRequest m_request = new UsbRequest();
m_request.Queue(m_rinfo, 64);<<error
The last line gives an error, "Use of assigned variable 'm_rinfo'
I've tried to assign bytes to m_rinfo with,
Java.Nio.ByteBuffer m_rinfo = new byte[64];
Which gives error, Cannot implicitly convert type byte
Any help would be appreciated.
I find I can convert the lines to;
Java.Nio.ByteBuffer m_rinfo = Java.Nio.ByteBuffer.Allocate(64);
Int32 m_bytes = 64;
UsbRequest m_request = new UsbRequest();
m_request.Queue(m_rinfo, 64);
However when the program gets to line;
m_request.Queue(m_rinfo, 64);
I receive the error;
'Java.Lang.RuntimeException' was thrown
So what's wrong with this line?

Have you tried m_request.Queue(out m_rinfo, 64);? As far as I'm aware, out is used when a variable in c# is passed to a method with the intent of its value being changed within it. For example:
ListOfSomeKind list = null;
methodToFillListWithData(out list);

Related

Calling Matlab function from .net(C#) by using MLApp.Feval()

I tried to use Matlab function from my .net project
function [HR] = getHR(signal)
Fs = 200;
index = pan_tompkin(signal,Fs);
index = index(2:end-1);
idx_dur = (index(end) - index(1))/Fs;
idx_cnt = length(index)-1;
idx_int = idx_dur/idx_cnt;
HR = round(60/idx_int);
end
This is my Matlab code
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(#"cd C:\Users\User\Desktop");
object result = null;
matlab.Feval("getHR", 1, out result,input_Data);
object[] res = result as object[];
tmp_HR = Convert.ToInt32(res[0]);
And this is part of my .net code where calling Matlab function
input_Data is 2000x1 double array
When I run this program, error is occur that "Undefined function 'getHR' for input arguments of type 'double'." on Matlab.Feval line
Someone advised me to 'varargin' to solve this problem, but I can not find the answer(I don't think it is necessary)
How can I revise my code to fix this problem?
I can not use the way to use Matlab compiler SDK or Matlab coder
maybe have to use only MLApp to solve this problem

How can I properly call emgucv retrieve command in Visual C++?

I'm trying to translate a piece of code from visual c# to visual c++ and can't seem to translate this bit:
Mat m = new Mat();
capture.Retrieve(m);
pictureBox1.Image = m.ToImage<Bgr, byte>().Bitmap;
so far i've written
Mat^ m = gcnew Mat();
Emgu::CV::Capture::Retrieve(m);
pictureBox1->Image = m->ToImage<Bgr, Byte>()->Bitmap;
but i get the following errors
(E1767 function "Emgu::CV::Capture::Retrieve" cannot be called with the given argument list)
(E0304 no instance of function template "Emgu::CV::Mat::ToImage" matches the argument list)
Any help would be appreciated.

Using C++ method in c#

I'm writing a c# console application that uses a C++ class library. In C++ language class library I have a method:
public:bool GetMDC(char fileName[], char mdcStrOut[]){
// My Code goes Here
}
This method gets a file path in fileName parameter and puts a value in mdcStrOut.
I add this class library as reference to my C# console application. when I want to call GetMDC method the method needs two sbyte parameters. So it's signature in c# is GetMDC(sbyte* fileName, sbyte* mdcStrOut).
My code looks like this:
unsafe{
byte[] bytes = Encoding.ASCII.GetBytes(fileName);
var _mdc = new TelsaMDC.TelsaMDCDetection();
var outPut = new sbyte();
fixed (byte* p = bytes)
{
var sp = (sbyte*)p;
//SP is now what you want
_mdc.GetMDC(sp, &outPut);
}
}
It works without error. But the problem is that outPut variable only contains the first character of mdcStrOut. I'm unfamiliar with C++. I know that I'm passing the memory address of output to the GetMDC. So how can I get the value of it in my console application?
EDIT
when I declare output variable like this var outPut = new sbyte[MaxLength] I get an error on _mdc.GetMDC(sp, &outPut); line on the & sign. It says: Cannot take the address of, get the size of, or declare a pointer to a managed type ('sbyte[]')
The variable outPut is a single byte.
You need to create a receive buffer, for example, var outPut = new sbyte[MaxLength].
unsafe{
byte[] bytes = Encoding.ASCII.GetBytes(fileName);
var _mdc = new TelsaMDC.TelsaMDCDetection();
var outPut = new sbyte[256]; // Bad practice. Avoid using this!
fixed (byte* p = bytes, p2 = outPut)
{
var sp = (sbyte*)p;
var sp2 = (sbyte*)p2;
//SP is now what you want
_mdc.GetMDC(sp, sp2);
}
}
Also, I recommend to rewrite the code to avoid possible buffer overflow because the function GetMDC does not know the size of the buffer.

I need to port the following from C# to C++/CX, but I get a given argument error

This is what the code in C# looks like:
FilterEffect effect = new FilterEffect();
LomoFilter lomoFilter = new LomoFilter();
VignettingFilter vignettingFilter = new VignettingFilter();
effect.Filters = new IFilter[] { lomoFilter, vignettingFilter };
List<IImageProvider> providers = new List<IImageProvider>();
providers.Add(effect);
testSet.Add(new KeyValuePair<string, object>("IImageProviders", providers));
This is the code I have for C++ so far:
PropertySet ^testSet = ref new Windows::Foundation::Collections::PropertySet();
FilterEffect effect = ref new FilterEffect();
LomoFilter ^lomo = ref new LomoFilter();
Platform::Collections::Vector<IFilter> ^vector = ref new Platform::Collections::Vector<IFilter>();
vector->Append(lomo);
effect.Filters = vector;
std::list<IImageProvider> providers;
I get the following error at build:
106 IntelliSense: function "Nokia::Graphics::Imaging::FilterEffect::Filters::set" cannot be called with the given argument list
argument types are: (Platform::Collections::Vector<Nokia::Graphics::Imaging::IFilter, std::equal_to<Nokia::Graphics::Imaging::IFilter>, <error-constant>> ^)
object type is: Nokia::Graphics::Imaging::FilterEffect ^ c:\Users\Alin Rosu\Documents\Visual Studio 2013\Projects\App2Good\AddEffectsBlaApp2\App2\App2\MainPage.xaml.cpp 113 7 App2
I'm new to c++/CX so I have no ideea what I have to do here, tried using a std::list instead of the vector, but still nothing. What should I do here, so it will pass the filters without having compiling errors.
IntelliSense errors are not compilation errors but IDE internal code analysis that may be correct but may be not. If you don't have actual compiler errors you can ignore those or disable them in context menu of errors window.
I forgot to put "^" at the second line:
FilterEffect ^effect = ref new FilterEffect();
Doing that the vector had no problem taking the value, and the code runs

Add Syntax Highlighting to IElisonBuffer

I'm working on a project where we've split up C# code into functions and stored these functions within IElisonBuffers. I've got Intellisense hooked up, and the buffers interact with other extensions fine as shown below:
However, I cannot get syntax highlighting to work within these editors.
I embed these editors via the following steps:
Create an IVsInvisibleEditor for the file.
Get the IVsTextLines for this IVsInvisibleEditor
Create an IVsCodeWindow and set the buffer for this IVsCodeWindow to be the IVsTextLines from the IVsInvisibleEditor
Get an IWpfTextViewHost from this code window. This brings me back to "WPF Land" where I'm able to interact with traditional spans.
Create a SnapshotSpan of the IWpfTextViewHost's text view. This SnapshotSpan contains a single function.
Create an IElisionBuffer containing the SnapshotSpan.
Create an IVsTextBuffer via IVsEditorAdaptersFactoryService.CreateVsTextBufferAdapterForSecondaryBuffer() passing in the IElisionBuffer.
Now I cast the IVsTextBuffer to IVsTextLines and call SetLanguageServiceID() passing in the C# GUID: 694DD9B6-B865-4C5B-AD85-86356E9C88DC.
I double check that it was set correctly via GetLanguageServiceID() and everything looks alright.
I create an IVsTextView and initialize it with the new IVsTextBuffer.
I then get the IWpfTextViewHost for this IVsTextView.
Are there any special steps that need to be taken care of when setting up the language service ID for an IElisionBuffer?
For the sake of completeness this is the code I'm using:
public CustomEditorViewModel CreateEditor(string filePath, int start, int end) {
IVsInvisibleEditor invisibleEditor;
ErrorHandler.ThrowOnFailure(this._InvisibleEditorManager.RegisterInvisibleEditor(
filePath
, pProject: null
, dwFlags: (uint)_EDITORREGFLAGS.RIEF_ENABLECACHING
, pFactory: null
, ppEditor: out invisibleEditor));
var docDataPointer = IntPtr.Zero;
Guid guidIVsTextLines = typeof(IVsTextLines).GUID;
ErrorHandler.ThrowOnFailure(
invisibleEditor.GetDocData(
fEnsureWritable: 1
, riid: ref guidIVsTextLines
, ppDocData: out docDataPointer));
IVsTextLines docData = (IVsTextLines)Marshal.GetObjectForIUnknown(docDataPointer);
//Createa a code window adapter
var codeWindow = _EditorAdapterFactory.CreateVsCodeWindowAdapter(VisualStudioServices.OLEServiceProvider);
//Associate our IVsTextLines with our new code window
ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(docData));
//Get our text view for our editor which we will use to get the WPF control that hosts that editor.
IVsTextView textView;
ErrorHandler.ThrowOnFailure(codeWindow.GetPrimaryView(out textView));
//This is our TextViewHost
//It transports us back into the land of WPF
IWpfTextViewHost textViewHost = _EditorAdapterFactory.GetWpfTextViewHost(textView);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Now we need to subset TextBuffer somehow...
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int length = end - start;
SnapshotSpan subsetSnapshot = new SnapshotSpan(textViewHost.TextView.TextSnapshot, start, length);
var CSharpType = _contentTypeRegistry.GetContentType("CSharp");
var projBuffer = _ProjectionBufferFactory.CreateElisionBuffer(
null
, new NormalizedSnapshotSpanCollection(subsetSnapshot)
, ElisionBufferOptions.None
,CSharpType);
IVsTextBuffer bufferAdapter = _EditorAdapterFactory.CreateVsTextBufferAdapterForSecondaryBuffer(VisualStudioServices.OLEServiceProvider, projBuffer);
//My attempt at getting syntax coloring to work:
Guid CSharpLanguageServiceId = new Guid("694DD9B6-B865-4C5B-AD85-86356E9C88DC");
IVsTextLines buffer = (IVsTextLines)bufferAdapter;
buffer.SetLanguageServiceID(ref CSharpLanguageServiceId);
IVsTextView projTextView = _EditorAdapterFactory.CreateVsTextViewAdapter(VisualStudioServices.OLEServiceProvider);
projTextView.Initialize(
(IVsTextLines)bufferAdapter
, IntPtr.Zero
, (uint)TextViewInitFlags.VIF_HSCROLL | (uint)TextViewInitFlags.VIF_VSCROLL | (uint)TextViewInitFlags3.VIF_NO_HWND_SUPPORT,
new[] { new INITVIEW { fSelectionMargin = 0, fWidgetMargin = 0, fVirtualSpace = 0, fDragDropMove = 0 } }
);
return _EditorAdapterFactory.GetWpfTextViewHost(projTextView);
}
Make the content type of your elision buffer be, or derive from, the content type "projection". That's the hint that taggers should project through that.

Categories

Resources