I'm trying to create a wrapper C# file for modbusm.dll (win-tech.com/html/mbusocx.htm) ,I'm using dumpbin output for this.
Dump of file modbusm.dll
File Type: DLL
Section contains the following exports for modbusm.dll
00000000 characteristics
41128817 time date stamp Fri Aug 06 00:48:47 2004
0.00 version
1 ordinal base
27 number of functions
27 number of names
ordinal hint RVA name
1 0 000085BA _AbortTheCall#4
2 1 00003441 _CloseConnection#4
3 2 000033A7 _ConnectASCII#12
4 3 000033E1 _ConnectDanielsASCII#12
5 4 000033C4 _ConnectDanielsRTU#12
6 5 0000338A _ConnectRTU#12
7 6 00001019 _ConnectTCP2#12
8 7 00001000 _ConnectTCP#8
9 8 0000829A _DialCall#8
10 9 00003376 _EnableConnectionCallback#4
11 A 00003342 _EnableModbusCallback#8
12 B 00008123 _GetCallState#8
13 C 00007FD2 _GetLineDeviceName#12
14 D 00003320 _GetPollDelay#0
15 E 00003339 _Get_Modbus_DLL_Revision#0
16 F 000033FE _HookRspNotification#16
17 10 000032ED _InitializeWinSock#0
18 11 0000277C _MBAPWndProc#16
19 12 0000393F _MODBUSResponse#16
20 13 00007EAA _NumberOfLineDevices#0
21 14 00003521 _PollMODBUS#8
22 15 000039F2 _ReadDebugData#16
23 16 00003BA4 _ReadTransparentResponse#16
24 17 0000332A _SetPollDelay#4
25 18 00003313 _UnInitializeWinSock#0
26 19 00003712 _WriteMODBUS#12
27 1A 00003AB3 _WriteTransparentString#12
Summary
5000 .data
2000 .rdata
2000 .reloc
1000 .rsrc
C000 .text
my C# wrapper is
class MbMasterV7
{
[DllImport("modbusm.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "_ConnectTCP2#12")]
public static extern int ConnectModbusTCP(short Port);
public static string TCPDevice { set; get; }
}
when I am running the code
MbMasterV7.TCPDevice = "127.0.0.1"; // from demo version of .ocx file converted using tlbimp.exe
MbMasterV7.ConnectModbusTCP(502);
in visual studio, I am getting exception
A call to PInvoke function
'TestApp!TestApp.MbMasterV7::ConnectModbusTCP' has unbalanced the
stack. This is likely because the managed PInvoke signature does not
match the unmanaged target signature. Check that the calling
convention and parameters of the PInvoke signature match the target
unmanaged signature.
I have tried all calling conventions and getting same error.
The .Net libraries are available for modbus protocol are not good enough for the new plc type I am working with.
FILE HEADER VALUES
14C machine (x86)
5 number of sections
41128817 time date stamp Fri Aug 06 00:48:47 2004
0 file pointer to symbol table
0 number of symbols
E0 size of optional header
210E characteristics
Executable
Line numbers stripped
Symbols stripped
32 bit word machine
DLL
OPTIONAL HEADER VALUES
10B magic # (PE32)
6.00 linker version
C000 size of code
A000 size of initialized data
0 size of uninitialized data
94E4 entry point (100094E4)
1000 base of code
D000 base of data
10000000 image base (10000000 to 10016FFF)
1000 section alignment
1000 file alignment
4.00 operating system version
0.00 image version
4.00 subsystem version
0 Win32 version
17000 size of image
1000 size of headers
0 checksum
2 subsystem (Windows GUI)
0 DLL characteristics
100000 size of stack reserve
1000 size of stack commit
100000 size of heap reserve
1000 size of heap commit
0 loader flags
10 number of directories
DF90 [ 357] RVA [size] of Export Directory
D7C8 [ 50] RVA [size] of Import Directory
14000 [ 3E8] RVA [size] of Resource Directory
0 [ 0] RVA [size] of Exception Directory
0 [ 0] RVA [size] of Certificates Directory
15000 [ BC0] RVA [size] of Base Relocation Directory
0 [ 0] RVA [size] of Debug Directory
0 [ 0] RVA [size] of Architecture Directory
0 [ 0] RVA [size] of Global Pointer Directory
0 [ 0] RVA [size] of Thread Storage Directory
0 [ 0] RVA [size] of Load Configuration Directory
0 [ 0] RVA [size] of Bound Import Directory
D000 [ 198] RVA [size] of Import Address Table Directory
0 [ 0] RVA [size] of Delay Import Directory
0 [ 0] RVA [size] of COM Descriptor Directory
0 [ 0] RVA [size] of Reserved Directory
First thing first, _ConnectTCP2#12 means that 12 bytes are passed as parameters to the function, which means that short (which is 2 bytes long) is obviously incompatible. You need to pass 12 bytes as parameters, presumably as 3 DWORDs.
Let us, for the sake of adventure, actually disassemble the binary to see what's going on there.
So: ConnectTCP#8 is receiving 2 DWORDs as arguments and calling ConnectTCP2 with 0x1F6 as the second parameter (which is actually a short). Also, the calling convention is stdcall.
That's enough information for us to figure out how to call the function:
[DllImport("modbusm.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "_ConnectTCP2#12")]
public static extern int ConnectModbusTCP(int a, short Port, int b);
Will work, but will throw
"Unhandled Exception: System.AccessViolationException:
Attempted to read or write protected memory.
This is often an indication that other memory is corrupt."
This is because the second integer (that I called b) is actually a pointer to a structure (which its values I can only guess according to the code). So let us reconstruct the structure. According to the code, the structure is accessed five times as following:
Offset Type
0x00 -> INT32
0x04 -> INT32
0x08 -> INT32
0x0C -> INT16
0x10 -> INT32
So, by creating the following structure:
struct MbMasterStruct
{
int a;
int b;
int c;
short d;
int e;
}
And redefining the function to be:
unsafe class MbMasterV7
{
[DllImport("modbusm.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "_ConnectTCP2#12")]
public static extern int ConnectModbusTCP(int a, short Port, MbMasterStruct * b);
}
And calling it as following:
static void Main(string[] args)
{
var structure = new MbMasterStruct();
unsafe
{
MbMasterV7.ConnectModbusTCP(1, 2, &structure);
}
}
It actually works, and it doesn't throw. On my computer, it returned 51 (when the structure and parameters were all zero).
It is now up to you to understand each parameters and see how to properly call the function.
Related
I have a BSOD 0x8E every day on five devices in same times on Windows 7.
Allways exception code - 04 - STATUS_SINGLE_STEP
as example:
1: kd> !analyze -v
*******************************************************************************
* *
* Bugcheck Analysis *
* *
*******************************************************************************
KERNEL_MODE_EXCEPTION_NOT_HANDLED_M (1000008e)
This is a very common bugcheck. Usually the exception address pinpoints
the driver/function that caused the problem. Always note this address
as well as the link date of the driver/image that contains this address.
Some common problems are exception code 0x80000003. This means a hard
coded breakpoint or assertion was hit, but this system was booted
/NODEBUG. This is not supposed to happen as developers should never have
hardcoded breakpoints in retail code, but ...
If this happens, make sure a debugger gets connected, and the
system is booted /DEBUG. This will let us see why this breakpoint is
happening.
Arguments:
Arg1: 80000004, The exception code that was not handled
Arg2: ffdff2a5, The address that the exception occurred at
Arg3: a9c34d48, Trap Frame
Arg4: 00000000
Debugging Details:
------------------
DUMP_CLASS: 1
DUMP_QUALIFIER: 400
BUILD_VERSION_STRING: 7601.17514.x86fre.win7sp1_rtm.101119-1850
SYSTEM_MANUFACTURER: System manufacturer
SYSTEM_PRODUCT_NAME: System Product Name
SYSTEM_SKU: SKU
SYSTEM_VERSION: System Version
BIOS_VENDOR: American Megatrends Inc.
BIOS_VERSION: 1002
BIOS_DATE: 06/04/2013
BASEBOARD_MANUFACTURER: ASUSTeK COMPUTER INC.
BASEBOARD_PRODUCT: P8H61-I LX R2.0
BASEBOARD_VERSION: Rev X.0x
DUMP_TYPE: 2
BUGCHECK_P1: ffffffff80000004
BUGCHECK_P2: ffffffffffdff2a5
BUGCHECK_P3: ffffffffa9c34d48
BUGCHECK_P4: 0
EXCEPTION_CODE: (HRESULT) 0x80000004 (2147483652) - <Unable to get error code text>
FAULTING_IP:
+0
ffdff2a5 0000 add byte ptr [eax],al
TRAP_FRAME: a9c34d48 -- (.trap 0xffffffffa9c34d48)
ErrCode = 00000000
eax=00000000 ebx=846410c0 ecx=00000000 edx=00000000 esi=0c4cdb04 edi=00000000
eip=ffdff2a5 esp=a9c34dbc ebp=801ef000 iopl=0 ov dn di ng nz na pe nc
cs=0008 ss=0010 ds=0023 es=0023 fs=0030 gs=0000 efl=00004c86
ffdff2a5 0000 add byte ptr [eax],al ds:0023:00000000=??
Resetting default scope
CPU_COUNT: 2
CPU_MHZ: a8c
CPU_VENDOR: GenuineIntel
CPU_FAMILY: 6
CPU_MODEL: 3a
CPU_STEPPING: 9
CPU_MICROCODE: 6,3a,9,0 (F,M,S,R) SIG: 17'00000000 (cache) 17'00000000 (init)
CUSTOMER_CRASH_COUNT: 1
DEFAULT_BUCKET_ID: BAD_STACK_0x8E
BUGCHECK_STR: 0x8E
PROCESS_NAME: avp.exe
CURRENT_IRQL: 0
ANALYSIS_SESSION_HOST: NETMANNOTE
ANALYSIS_SESSION_TIME: 11-17-2017 12:11:18.0584
ANALYSIS_VERSION: 10.0.15063.468 x86fre
LAST_CONTROL_TRANSFER: from 00000000 to ffdff2a5
STACK_TEXT:
a9c34db8 00000000 0000027f 00000000 00000000 0xffdff2a5
STACK_COMMAND: .trap 0xffffffffa9c34d48 ; kb
SYMBOL_NAME: ANALYSIS_INCONCLUSIVE
FOLLOWUP_NAME: MachineOwner
MODULE_NAME: Unknown_Module
IMAGE_NAME: Unknown_Image
DEBUG_FLR_IMAGE_TIMESTAMP: 0
BUCKET_ID: BAD_STACK_0x8E
PRIMARY_PROBLEM_CLASS: BAD_STACK
FAILURE_BUCKET_ID: BAD_STACK_0x8E
TARGET_TIME: 2017-11-16T13:53:07.000Z
OSBUILD: 7601
OSSERVICEPACK: 1000
SERVICEPACK_NUMBER: 0
OS_REVISION: 0
SUITE_MASK: 336
PRODUCT_TYPE: 1
OSPLATFORM_TYPE: x86
OSNAME: Windows 7
OSEDITION: Windows 7 WinNt (Service Pack 1) TerminalServer EmbeddedNT SingleUserTS
OS_LOCALE:
USER_LCID: 0
OSBUILD_TIMESTAMP: 2010-11-20 11:42:49
BUILDDATESTAMP_STR: 101119-1850
BUILDLAB_STR: win7sp1_rtm
BUILDOSVER_STR: 6.1.7601.17514.x86fre.win7sp1_rtm.101119-1850
ANALYSIS_SESSION_ELAPSED_TIME: cc3
ANALYSIS_SOURCE: KM
FAILURE_ID_HASH_STRING: km:bad_stack_0x8e
FAILURE_ID_HASH: {993602b9-6aa7-fbab-86a8-bb6ad81a3997}
Followup: MachineOwner
The service name can be different: av.exe, scvhost.ext,..
I know that is due to the a driver does not process STATUS_SINGLE_STEP exception.
Is any idea how to find wrong driver? There is no stack and module name in the dump...
I tried to run the Mirosoft Driver Verifier with standart flags and all drivers without Microsoft drivers, dump listing is above.
I want to get the following information about the disk performance.
I used MSStorageDriver_FailurePredictThresholds, MSStorageDriver_ATAPISmartData, MSStorageDriver_FailurePredictStatus class to get relative information but not getting the right result.
This isn't everything you wanted, but the Win32_PerfFormattedData_PerfDisk_PhysicalDisk WMI class gives you current read / write / transfer rate and activity time. It also gives some averages but I'm not sure how they are calculated (it seems odd that AvgDiskBytesPerWrite is 0)
// NOTE: Use ManagementObjectSearcher to find the path your interested in
var path = "Win32_PerfFormattedData_PerfDisk_PhysicalDisk.Name='0 C:'";
var wmiObj = new ManagementObject(path);
wmiObj.Properties.Cast<PropertyData>().ToDictionary(p => p.Name, p => p.Value).Dump();
/* OUTPUT */
AvgDiskBytesPerRead 63167
AvgDiskBytesPerTransfer 63167
AvgDiskBytesPerWrite 0
AvgDiskQueueLength 0
AvgDiskReadQueueLength 0
AvgDisksecPerRead 0
AvgDisksecPerTransfer 0
AvgDisksecPerWrite 0
AvgDiskWriteQueueLength 0
Caption null
CurrentDiskQueueLength 0
Description null
DiskBytesPersec 20991616
DiskReadBytesPersec 20991616
DiskReadsPersec 332
DiskTransfersPersec 332
DiskWriteBytesPersec 0
DiskWritesPersec 0
Frequency_Object null
Frequency_PerfTime null
Frequency_Sys100NS null
Name 0 C:
PercentDiskReadTime 80
PercentDiskTime 80
PercentDiskWriteTime 0
PercentIdleTime 32
SplitIOPerSec 0
Timestamp_Object null
Timestamp_PerfTime null
Timestamp_Sys100NS null
if you looking for this, check this Link. An example of output is:
###############################################################
Current Directory Path: D:\WinDDK\32bit\drivespeed
Total MB 230000, Free MB 103752, Used MB 126248
Windows Storage Speed Test 32-Bit Version 1.2, Thu Mar 01 22:31:08 2012
Copyright (C) Roy Longbottom 2011
8 MB File 1 2 3 4 5
Writing MB/sec 56.68 105.37 61.95 72.48 75.33
Reading MB/sec 80.65 108.15 81.10 81.42 82.25
16 MB File 1 2 3 4 5
Writing MB/sec 81.53 94.61 81.41 88.06 71.39
Reading MB/sec 88.25 72.75 93.53 93.51 92.70
32 MB File 1 2 3 4 5
Writing MB/sec 90.35 83.60 72.52 80.24 72.71
Reading MB/sec 87.00 87.67 78.39 80.24 78.62
---------------------------------------------------------------------
8 MB Cached File 1 2 3 4 5
Writing MB/sec 703.27 628.08 1050.99 1617.29 1609.70
Reading MB/sec 1930.60 2045.13 2054.49 2135.91 2390.08
---------------------------------------------------------------------
Bus Speed Block KB 64 128 256 512 1024
Reading MB/sec 174.36 189.07 221.83 247.82 261.21
---------------------------------------------------------------------
1 KB Blocks File MB > 2 4 8 16 32 64 128
Random Read msecs 0.14 0.14 0.15 0.17 3.24 6.96 8.90
Random Write msecs 0.53 0.66 1.03 1.38 1.74 1.83 2.42
---------------------------------------------------------------------
500 Files Write Read Delete
File KB MB/sec ms/File MB/sec ms/File Seconds
2 0.32 6.44 0.95 2.17 0.123
4 6.49 0.63 12.21 0.34 0.113
8 11.54 0.71 15.17 0.54 0.121
16 22.04 0.74 41.78 0.39 0.116
32 32.92 1.00 26.31 1.25 0.074
64 68.72 0.95 51.88 1.26 0.132
End of test Thu Mar 01 22:32:11 2012
I have a WPF application that works fine on certain platforms and systems, including, of course, the development system, however, it runs into issues on some, but not all, platforms on deploy. This sounds like an issue with regard to dependencies, of course, but I'm not sure how to track down what/which.
The WERInternalMetadata.xml problem signature shows:
<ProblemSignatures>
<EventType>CLR20r3</EventType>
<Parameter0>QroLockdownShell.exe</Parameter0>
<Parameter1>1.0.0.0</Parameter1>
<Parameter2>559aaa45</Parameter2>
<Parameter3>mscorlib</Parameter3>
<Parameter4>4.0.30319.34209</Parameter4>
<Parameter5>534894cc</Parameter5>
<Parameter6>4527</Parameter6>
<Parameter7>105</Parameter7>
<Parameter8>System.Windows.Markup.XamlParse</Parameter8>
</ProblemSignatures>
When I use ILDASM to look for 105 from Parameter 7, it leads me to this:
Method #19 (06000105)
-------------------------------------------------------
MethodName: get_BackgroundImage (06000105)
Flags : [Public] [HideBySig] [ReuseSlot] [SpecialName] (00000886)
RVA : 0x00004478
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Class System.Windows.Media.ImageSource
No arguments.
CustomAttribute #1 (0c0000ed)
-------------------------------------------------------
CustomAttribute Type: 0a00002f
CustomAttributeName: System.Runtime.CompilerServices.CompilerGeneratedAttribute :: instance void.ctor()
Length: 4
Value : 01 00 00 00 > <
ctor args: ()
I'm not sure how to make heads or tails of this...I would be very, very thankful for any help!
I've concatenated a series of VOB files from a DVD into a single VOB file and I am trying to convert it to MP4 or other similar format. I see a lot of errors when converting and the output appears scrambled.
>ffmpeg.exe" -i file.vob -sameq file.mp4
FFmpeg version git-N-29181-ga304071, Copyright (c) 2000-2011 the FFmpeg develope
rs
built on Apr 18 2011 21:24:03 with gcc 4.5.2
configuration: --enable-gpl --enable-version3 --enable-runtime-cpudetect --ena
ble-memalign-hack --enable-avisynth --enable-bzlib --enable-frei0r --enable-libo
pencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enable-libgsm --
enable-libmp3lame --enable-libopenjpeg --enable-librtmp --enable-libschroedinger
--enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enabl
e-libx264 --enable-libxavs --enable-libxvid --enable-zlib --cross-prefix=i686-w6
4-mingw32- --target-os=mingw32 --arch=x86_32 --extra-cflags=-I/home/kyle/softwar
e/ffmpeg/external-libraries/win32/include --extra-ldflags=-L/home/kyle/software/
ffmpeg/external-libraries/win32/lib --pkg-config=pkg-config
libavutil 50. 40. 1 / 50. 40. 1
libavcodec 52.120. 0 / 52.120. 0
libavformat 52.108. 0 / 52.108. 0
libavdevice 52. 4. 0 / 52. 4. 0
libavfilter 1. 79. 0 / 1. 79. 0
libswscale 0. 13. 0 / 0. 13. 0
[mpeg2video # 01751A90] ac-tex damaged at 5 16
[mpeg2video # 01751A90] invalid mb type in I Frame at 0 1
[mpeg2video # 01751A90] invalid mb type in I Frame at 0 2
[mpeg2video # 01751A90] invalid mb type in I Frame at 0 3
[mpeg2video # 01751A90] invalid mb type in I Frame at 0 4
[...]
I'm guessing this is CSS scrambling and I need to do some sort of DeCSS. Does FFMpeg have an option for this? Cringe if you'd like, but is there C# source to achieve this?
My end goal is really just to get some DVDs that I own onto my media server. I've tried a few demo-ware products with limited success including Acala DVD Ripper, Click to Disk, AVIDemux to name a few. I even paid for a couple of them to get the full version, but Acala only works for about half of my DVDs and Click to Disk can decode the VOB, but I need to use FFMpeg to convert. I'd like to have it all in one app that works and I am willing to write some code for it.
We have large executable files (>1,2 GB) which contain custom Version Info.
I try to retrieve this 'Version Info' from these files by using the 'GetVersionInfo' of the 'FileVersionInfo' Class.
For some reason this method doesn't return the version information for larger files (Tested with > 1 GB) in Windows XP. It did work for a file with a size of 18 MB.
When I try the same in Windows 7 (x86 and X64) it works for all files, even the larger onces!
I used a Reflector tool to take a look into the FileVersionInfo class and I've created a small console app to retrieve the 'File Version Info'-Size,
just as the 'GetVersionInfo' method does. A size of 0 (zero) is returned in Windows XP and in Windows 7 a size of 1428 is returned for the same files. The Last Error in XP is 1812 ('The specified image file did not contain a resource section').
What's the reason why this doesn't work in Windows XP and does work in Windows 7?
Is there a work around to retrieve the Version Info?
Below the code I've tested with:
class Program
{
[DllImport("version.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetFileVersionInfoSize(string lptstrFilename, out int handle);
static void Main(string[] args)
{
Console.Write("Path which contains the executables: ");
string path = Console.ReadLine();
foreach (string fileName in Directory.EnumerateFiles(path))
{
int num;
int fileVersionInfoSize = GetFileVersionInfoSize(fileName, out num);
int error = Marshal.GetLastWin32Error();
Console.WriteLine("File Version Info Size: " + fileVersionInfoSize);
if (error != 0)
{
Console.WriteLine("Last Error: " + error);
}
}
Console.ReadKey();
}
}
The loader probably failed to map the entire file into its address space. Feel free to read up on the PE file format to find the version resource.
However, what are you doing with such a large PE Image? If its custom resources, it works better to append them to the .exe so the loader only has to map a little bit, then access them directly. This requires you to know your size (there are 4 bytes at offset 124 that can be overwritten safely as they are pad after the "This Program cannot be run in MS-DOS mode." error message stub).