So tell me about this C#-VBForums
Results 1 to 8 of 8

Thread: So tell me about this C#

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    15,817

    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??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,600

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    15,817

    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!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    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#.
    W o t . S i g

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    15,817

    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!)?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    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};
    W o t . S i g

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    15,817

    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...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    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).
    W o t . S i g

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.