[RESOLVED] Calling a C/C++ string search function from a VB thread
I created a STRING SEARCH function in C/C++ - and I'm trying to use it in a multi-threaded situation in VB.
I started this problem in a thread in the C/C++ section of the forum - here
http://www.vbforums.com/showthread.php?t=675943
At any rate - my last post shows me getting this error trying to call the function.
Ran the code - got to that point - and in the immediate window I did
Code:
?firstIndexOfKeyword("i had fancy italian food for lunch", {"meh", "feh", "italian"}, 3)
{"An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred"}
_HResult: -2146232832
_message: "Error in the application."
Data: {System.Collections.ListDictionaryInternal}
HelpLink: Nothing
HResult: -2146232832
InnerException: Nothing
IsTransient: False
Message: "An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred"
Source: "Microsoft.VisualStudio.Debugger.Runtime"
StackTrace: " at Microsoft.VisualStudio.Debugger.Runtime.Main.ThrowCrossThreadMessageException(String formatString) at System.Windows.Forms.SafeNativeMethods.GetWindowTextLength(HandleRef hWnd) at System.Windows.Forms.Control.get_WindowText() at System.Windows.Forms.Form.get_WindowText() at System.Windows.Forms.Control.get_Text() at System.Windows.Forms.Form.get_Text() at System.Windows.Forms.Form.ToString()"
TargetSite: {Void ThrowCrossThreadMessageException(System.String)}
And got a pop-up error about not finding the ENTRY point and this ugly cross threading error...
Re: Calling a C/C++ string search function from a VB thread
So, does the code work correctly when used in a single thread? What about in multiple threads? Is it just when you use the Immediate window that the issue arises? The issue appears to be that ToString is being called on a Form from a background thread, but I'm not 100% sure where that would be occurring.
Re: Calling a C/C++ string search function from a VB thread
Ok - I took this issue out of the immediate window - don't need to add more to my problem then I need
Took the spot where I was doing the breakpoint - and instead of the immediate window I added this code to call the firstIndexOfKeyword() function
Code:
Private Sub ScanFile(ByVal FSObParam As Object)
Dim x As Object = firstIndexOfKeyword("i had fancy italian food for lunch", {"meh", "feh", "italian"}, 3)
Dim FSOb As New FSObject
FSOb = DirectCast(FSObParam, FSObject)
and I am getting this error
Code:
System.EntryPointNotFoundException was unhandled
Message=Unable to find an entry point named 'firstIndexOfKeyword' in DLL 'D:\ACS Desktop\dcx\dcx\Debug\StringLibrary.dll'.
Source=Librarian
TypeName=""
StackTrace:
at Librarian.Librarian.firstIndexOfKeyword(String s, String[] substr, Int32 substrLength)
at Librarian.Librarian.ScanFile(Object FSObParam) in D:\ACS Desktop\dcx\dcx\Librarian\Librarian.vb:line 145
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)
InnerException:
I am sure I am missing something from the C/C++ project - first time trying to work with C/C++.
This is in a file called StringLibrary.cpp
Code:
// StringLibrary.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
__declspec(dllexport) int firstIndexOfKeyword(char *str, char **keywords, int nKeywords) {
int i, j, k;
int keywordlen;
int len = strlen(str);
for(k = 0; k < nKeywords; k++)
{
keywordlen = strlen(keywords[k]);
for(i = 0; i <= len - keywordlen; i++)
{
for(j = 0; j < keywordlen; j++)
{
if(keywords[k][j] != str[i + j])
j = keywordlen+1;
}
if(j == keywordlen)
return i;
}
}
return -1;
}
Re: Calling a C/C++ string search function from a VB thread
That error message certainly makes it sound like the function is not exported correctly in the C++ project but it's far too long since I wrote any C++ to be able to help in that regard I'm afraid. I see that Atheist got it working so I'm not sure what the issue might be.
Re: Calling a C/C++ string search function from a VB thread
This issue is resolved - refer to the other thread in the first post to see solutuions.