Solved

How to modify marshall commands for .NET 4.5.1

Posted on 2014-02-09
11
360 Views
Last Modified: 2014-02-15
Hello, it turns out I broke the following code (throws an exception on construction) when I moved up from .NET 4.5 to .NET 4.5.1.

        <System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)> Public Structure TimerCaps

            Public periodMin As Integer
            Public periodMax As Integer

        End Structure

            <System.Runtime.InteropServices.DllImport("winmm.dll")> _
            Private Shared Function timeGetDevCaps(ByRef caps As TimerCaps, ByVal sizeOfTimerCaps As Integer) As Integer
            End Function

            Shared Sub New()
                ' Get multimedia timer capabilities.
                timeGetDevCaps(caps, System.Runtime.InteropServices.Marshal.SizeOf(caps))
            End Sub

Open in new window


I found this issue report, which seems like a good match, except I don't know how to modify the above code analogously to the "type" cast additions in the solution.  Mine is a structure, not a type.

http://stackoverflow.com/questions/20376324/p-invoke-method-broken-just-by-updating-from-net-4-5-to-net-4-5-1

Can anyone suggest a change to my code to make it 4.5.1 compliant, assuming that the article applies?

Thanks very much.
0
Comment
Question by:RonMexico
  • 5
  • 5
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39847942
What is the exception that you are getting?
0
 

Author Comment

by:RonMexico
ID: 39847971
To be honest I'm not entirely sure I was seeing the proper message, it was along the lines of "type initializer threw an exception" when I was "new"ing an object with the members above.  It did go away when I rolled back from 4.5.1, and I boiled it down to the lines of code above, so it is a pretty good match with the article (which says they overloaded that call, so you need to be more specific when you call it).

Another weird thing is that it happened on some machines and not others (well, at least not mine, the development machine).
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39848104
What is that code doing?  

Do you need something from 4.5.1, or are you able to stick with 4.5?
0
 

Author Comment

by:RonMexico
ID: 39848252
It is getting the timer capabilities of a system timer accessed via winmm.dll.  Apparently this needs a structure passed in and out, and this requires marshalling.

I have rolled back to 4.5 for the time being, but it has been *very* useful code (provides much better timing, suitable for animation, than the .net timer) and I would like to get it working for 4.5.1 (and future).
0
 

Author Comment

by:RonMexico
ID: 39848257
By the way as you can tell I'm weak on marshaling, I got the wrapper from code project or something...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 96

Expert Comment

by:Bob Learned
ID: 39848483
There are three different timer types that you can use:

Comparing the Timer Classes in the .NET Framework Class Library
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

System.Windows.Forms.Timer
System.Timers.Timer
System.Threading.Timer


What is it about the unsafe timer that you need?
0
 

Author Comment

by:RonMexico
ID: 39848662
I have actually seen that article and tested each of those timers against the winmm.dll timer and at the time I ran the test it was no contest, for whatever reason it gives me much smoother animation, so I built an animation engine around it.  It works really well, surprises people that a forms application can do that.  

The article suggests that my marshalling calls can be disambiguated and my code can be made to work with 4.5.1... is it unclear to you (like me) how to do that?  Are you thinking of some other reason not to use my timer?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39848705
I am always interested in learning new things, so I was curious what you had discovered about the unsafe timer in winmm.dll.

Is this a 64-bit or 32-bit development platform?  

Where does this application run from?

I have access to both 32-bit and 64-bit platforms, but I don't have the time to test 4.5.1 against the marshal code until later.
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
ID: 39848706
It seems, according to documentation, that I have .NET 4.5.1 installed (on Windows 7), and your code worked as-is for me. Is this on Windows 8?
0
 

Author Comment

by:RonMexico
ID: 39848884
@kaufmed: That's interesting, thanks for trying.   It worked on my machine, which had Visual Studio 2013 (.NET 4.5.1) installed, and did not work on other machines that didn't have VS installed.  All machines were windows 7, and I upgraded to the latest framework on one of the other machines.

@TLO: all machines were 64 bit.  I just double click the EXE after putting on the desktop on the other machine.

I was just looking for syntax suggestions, how to cast per the article from someone who has more experience with the marshal functions -- but I definitely appreciate the add'l insight.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 375 total points
ID: 39852176
Here is the TIMECAPS structure:

typedef struct timecaps_tag {
  UINT wPeriodMin;
  UINT wPeriodMax;
} TIMECAPS, *PTIMECAPS, *NPTIMECAPS, *LPTIMECAPS;

Open in new window


http://msdn.microsoft.com/en-us/library/windows/desktop/dd757625(v=vs.85).aspx

Try changing the types to UInteger.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now