Declaring Win32 API functions with void callback parameters
-
I am trying to figure out how to declare a Win32 API function which takes a void-returning callback function as a parameter in Visual Basic 6. I'm writing a program that uses the Win32 API for asynchronous file I/O, specifically the ReadFileEx() function. To use this in Visual Basic 6 I need to declare it: Public Declare Function ReadFileEx Lib "kernel32" _ (ByVal hFile As Long, lpBuffer As Any, _ ByVal nNumberOfBytesToRead As Long, _ lpOverlapped As OverLapped, _ ByVal lpCompletionRoutine As Long) As Long The problem I am running into is that the callback function parameter (lpCompletionRoutine) will be a function that returns void, rather than a long (as it is specified above). Visual Basic 6 has no void type, functions that do not return anything are declared with "Sub" (which doesn't work as a return type, unfortunately). When I run my code with the declaration above, the function is called successfully, but the first two parameters passed to my callback are invalid and cause the whole program to crash when accessed. How can I change the ReadFileEx declaration above to specify the return type of the callback function as void?
-
Answer:
I think your current declaration is correct; see for example http://www.mvps.org/vbnet/index.html?code/enums/enumrestorewin.htm which uses the same method of declaring the callback parameter as "Long". I also found a product support article (http://support.microsoft.com/default.aspx?scid=kb;EN-US;q189850) which discusses using callbacks with WinINet APIs from Visual Basic and, after explaining some tricks to make it work, says "At this point, it should be noted that the necessary skill set to accomplish asynchronous WinInet APIs within Visual Basic will be the same as the skill set needed to write a C/C++ application using WinInet in asynchronous mode. This makes using WinInet APIs in Visual Basic asynchronously an undesirable option." I would tend to say the same thing about ReadFileEx: if you're really writing a multithreaded program that needs the performance gains from overlapped I/O, Visual Basic may not be the proper tool for the job. If there's a specific reason you really want to be doing this, I'll try to help further, though; let me know. You mentioned that using the "first two" parameters to your callback function causes your program to crash.... did you really mean that the lpOverlapped parameter is broken, and not the other two? I can't imagine how looking at either the error code or BytesRead could cause a crash. So I think that the problem isn't in the declaration, but somewhere else. Sorry I can't be more directly helpful, but I hope this information is useful to you. --David Search terms used: "readfileex visual basic" "void callback visual basic"
theoldpond-ga at Google Answers Visit the source
Related Q & A:
- How to pass multiple parameters in a single Ajax function?Best solution by stackoverflow.com
- Why is this jquery callback for complete not executed?Best solution by Stack Overflow
- How to call jQuery function with multiple parameters?Best solution by Stack Overflow
- How can I attach some parameters with request?Best solution by Stack Overflow
- Do piecewise functions have parent functions?Best solution by Ask.Metafilter.Com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.