-
Apr 29th, 2012, 03:04 PM
#1
So tell me about this C#
So - I've been using only VB.Net - but now find I need to call C++ functions for faster and customized string search and binary searching functions.
So - this C# - is that the C-syntax version of VB.Net - where I would make a UI app or a "windows service" app?
Similar form events and other events (such as windows service events) as VB.Net?
Is that basically the case??
-
Apr 29th, 2012, 06:31 PM
#2
Re: So tell me about this C#
Correct. If you create a C# project then you will proceed in pretty much exactly the same way as you do in VB.NET, except with syntax based on C/C++ instead of VB.
Calling functions from an unmanaged C++ library is not going to be any easier in C# than in VB.NET though, so if that's why you're considering C# then don't bother. The two have almost the same feature set and calling unmanaged functions is done using the DllImport attribute in both. VB also supports the old Declare syntax but that should not be used anyway.
One feature that C# does support that VB doesn't is unsafe code, i.e. pointers. As such, you may be able to reimplement the functionality from the C++ library using C# and avoid unmanaged code altogether. That would be a good reason to use C# but, even then, you could simply create a library in C# containing only the unsafe code and then reference that from a VB app.
-
Apr 29th, 2012, 06:43 PM
#3
Re: So tell me about this C#
You caught me!
I was hoping to make my C++ interaction easier with C#...
Oh well - no reason to learn a whole new syntax on the UI level.
Thanks!
-
May 5th, 2012, 07:21 PM
#4
Re: So tell me about this C#
Bit late to the show.
Have you had a look at C++ /CLI? It adds gcnew and ^'s for gc handles. You can use it to wrap your performance unmanaged code in a nice managed wrapper you can access without p/invoke. Caveats: You need to do your own marshalling, and there is no intellisense in the IDE. Pros: Faster than p/invoke (no marshalling) and much nicer to use from within VB or C#.
-
May 5th, 2012, 07:24 PM
#5
Re: So tell me about this C#
No - I had not heard about that.
Any links you can give me to look at - any pointers (no pun intended!)?
-
May 5th, 2012, 07:56 PM
#6
Re: So tell me about this C#
Not sure about pointers, how about a caret? (I think we are supposed to call them 'hats')
The horses mouth is always a good start
Code:
array<int>^myIntArray = {1,2,3,4,5};
-
May 6th, 2012, 06:42 AM
#7
Re: So tell me about this C#
Wow - confusing to say the least!
Seems that after I read that and a few other links - and a chapter from a Manning book that C++/CLI is all about giving a tool to C++ developers to come in and develop software that can talk to the .Net framework.
Let me explain my task a bit further and hopefully you can tell me if I should explore this C++/CLI further.
I've got a VB app - actually it will be a SERVICE when I'm done - but it's a Winform at the moment for proof-of-concept and demo'ing.
And it's VB.Net...
It sits there waiting for a document (file - WORD or PDF or whatever) to get sent to it (via httplistener code I've created).
Once it receives the document it needs to search for keywords and patterns in the "text" of that document.
This is where VB.Net string manipulation tanked on me - and I started using calls to simple C++ functions that could loop through a STRING (of the whole document contents) and also loop through an array of "keywords and rules" to find out if we have MARKER's in the text for certain keywords.
Here's an example of one of these C++ functions
Code:
__declspec(dllexport) int firstKeyword(char *strSearch, char **keywords, int nKeywords, int *markers) {
int i = 0, j = 0, k = 0, a = 0;
int keywordlen = 0;
int lenSearch = strlen(strSearch);
for(k = 0; k < nKeywords; k++) // Step through the keywords array
{
//keywordlen = strlen(keywords[k]); // Length of the keyword
for(i = 1; i <= lenSearch - keywords[k][0]; i++) // Loop through each character of the search pool ("short of end" by keyword length)
{
a = 0;
for(j = 0; j < keywords[k][0]; j++) // Loop through each character of the keyword
{
if(keywords[k][j+1] != strSearch[i + j]) // Non-match
{
j = keywords[k][0]+1; // Set j "longer" than keyword length
}
}
if(j == keywords[k][0]) // If j "equals" keyword length we have a match
{
markers[k] = i; // Set the Marker to the "location" of the first character
}
}
}
return -1; // Return something
}
and it's called from VB like this
Code:
Dim sTerms As String() = m_dcxE.KeyTerms
Dim sIds As Integer() = m_dcxE.KeyIds
Dim kc As Integer = sTerms.Count
Dim iMarkers(kc - 1) As Integer
For i As Integer = 0 To kc - 1
iMarkers(i) = -1
Next
Dim allText As String = ParsePdfText(FSOb.NewFileName)
Dim x As Object = firstKeyword(allText, sTerms, kc, iMarkers) ' <<<< C++ function call here
Dim tf As Integer = 0
For i As Integer = 0 To kc - 1
If iMarkers(i) <> -1 Then
FSOb.FCTags = New FSObject.dcxT(sIds(i), sTerms(i).Substring(1), "")
tf += 1
End If
Next
Once the function returns the VB.Net code looks at the array of markers and sees which ones were found.
Long story short - I'm going to needs quite a few of these C++ functions and the info I need to pass from VB to C++ is getting more complex as I move past proof-of-concept phase here.
When I asked this question originally I was wondering if using C# would make the parameter passing mechanism that much easier than doing it with VB.
Remember SPEED is PARAMOUNT here - I can suffer with parameter setup. Bottom line is that I want that MARKER array filled in ms's and not seconds...
-
May 6th, 2012, 07:16 PM
#8
Re: So tell me about this C#
The Marshalling between the managed Strings and the char* you have going on might be a little costly. I've a feeling that every call will involve copying all of those strings. Strings in .NET are immutable objects. If you could work it with Char or Byte arrays on the .NET side the calling cost will be cheaper. That said your function looks pretty intense with its nested loops so it would probably make little difference.
We had a look at wrapping a very simple C++ class library in a C++/CLI class library earlier this week, it was fairly straight forward. We were working with arrays which are simpler than managed Strings to deal with. Why not have a go at a simple hello world class library to see if you like it.
Going even further off topic, looking at your function, I can't help feeling that you might be able to optimise it. It seems very intense at first glance. Can you tell us any more about the "keywords and rules"? (in another thread if that is more appropriate).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|