NOTE: This page has been translated automatically from Russian to English. Original page.



Calling functions in Win 32 API 1C

Experiments with Win 32 API functions in 1C

And what does it do? EXPERIMENTS!

Certainly not 1C allows the user to use the functions 32 Win API. This opportunity can be solved by using ActiveX OLE-client such as dynwrap.dll or dynwrapx.dll, and dynwraph.dll much interested in their capabilities.

A little history: dynwrap.dll Library is the result of work of several developers, and was released 10 years ago. The main purpose of the call functions in Win 32 API WHS scripts. In September 2008, a new extended version of dynwraph.dll library, the example of which we use and stop.

An example of using the library to send messages to the mailslot \ messngr ala Net Send:

For a start it will register:
regsvr32.exe <path-to-component> \ dynwrapx.dll - for all users.
regsvr32.exe / i <path-to-component> \ dynwrapx.dll - for the current user.

Using 1C - Wrap = CreateObject ( "DynamicWrapperX"); register further function calls

Wrap.Register ( "KERNEL32.DLL", "CreateFile", "i = sllllll", "r = l");
Wrap.Register ( "KERNEL32.DLL", "WriteFile", "i = lllll", "r = l");
Wrap.Register ( "KERNEL32.DLL", "CloseHandle", "i = l", "r = l");

Initialize variables

GENERIC_READ = 2147483648;
GENERIC_WRITE = 1073741824;
FILE_SHARE_READ = 1;
FILE_SHARE_WRITE = 2;
OPEN_EXISTING = 3;
FILE_ATTRIBUTE_NORMAL = 128;
MessageText = AnsiToOem ( "Hello!");
AddFrom = AnsiToOem ( "From friends");
AddTo = "SERVER";
MailslotName = "\\" + AddTo + "\ mailslot \ messngr";
If Wrap <> 0 Then
hFile = Wrap.CreateFile (MailslotName, GENERIC_WRITE, FILE_SHARE_READ, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
If hFile> 0 Then
// MSG structure must be
// FromName + CHR (0) + ToComp + CHR (0) + MSG + CHR (0) + CHR (0)
// Length of no more than 424 characters
MSG = Wrap.Space (425);

Be sure to memorize the variable reference
pStr = Wrap.StrPtr (MSG);
BytesBeSend = 0;
For i = 1 By StrDlina (AddFrom) Cycle
Wrap.NumPut (KodSimv (Avg (AddFrom, i, 1)), pStr, BytesBeSend, "b");
BytesBeSend = BytesBeSend +1;
KonetsTsikla;
Wrap.NumPut (0, pStr, BytesBeSend, "b");
BytesBeSend = BytesBeSend +1;
For i = 1 By StrDlina (AddTo) Cycle
Wrap.NumPut (KodSimv (Avg (AddTo, i, 1)), pStr, BytesBeSend, "b");
BytesBeSend = BytesBeSend +1;
KonetsTsikla;
Wrap.NumPut (0, pStr, BytesBeSend, "b");
BytesBeSend = BytesBeSend +1;
For i = 1 By StrDlina (MessageText) Cycle
Quds = KodSimv (Avg (MessageText, i, 1));
Wrap.NumPut (Qods, pStr, BytesBeSend, "b");
BytesBeSend = BytesBeSend +1;
KonetsTsikla;
Wrap.NumPut (0, pStr, BytesBeSend, "b");
ByesBeSend = BytesBeSend +1;
Wrap.NumPut (0, pStr, BytesBeSend, "b");
BytesBeSend = BytesBeSend +1;
cbWritten = "";
pcbWritten = Wrap.StrPtr (cbWritten);
fResult = Wrap.WriteFile (hFile, pStr, BytesBeSend, pcbWritten, 0);
Wrap.CloseHandle (hFile);
If fResult = 1 Then
cbWritten = Wrap.NumGet (pcbWritten, 0, "l");
If cbWritten> 0 Then
Report ( "Delivered");
otherwise
Report ( "Message not sent");
ENDIF;
ENDIF;

That's the whole message was sent or not sent :-) the most important thing when using the function returns a value, or transmitted using the transmission / reception structures in this example is a pointer to a string containing the 3 section From To report after each symbol 0 and at the end of a leading 0. This structure establish regular means 1C "very difficult".

This is only one example. In MSDN Library, you can find a variety of useful functions.

This example allows you to implement the "file copy" at what experience has shown the maximum speed is achieved by using buffer data 65535 bytes in this track may copy (%, multiple progress bars, a resume), and of course do not forget about offloading CPU 1 ms.

Example №2 Obtaining absolute coordinates of the window

Register a function

Wrap.Register ( "USER32.DLL", "GetWindowRect", "i = lp", "r = l");
1 parameter is a window handle (you can find at least 2 options 1 title, Grade 2) or use the VC Formex (respect! Developer)
2 link to a Rect structure (type)
typedef struct _RECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, * PRECT;

1c is a bit kuzyavo
Rect = Wrap.Space (256); // Must be 4 DWord
Be sure to keep a pointer to the variable
pStr = Wrap.StrPtr (Rect);
Wrap.GetWindowRect (DiskriptorOkna, Rect);
Considering that this type of data in 1C does not exist, Rect 1C killed - but go for the saved pointer
and shall be deducted only because Rect
left = Wrap.NumGet (pStr, 0, "l");
top = Wrap.NumGet (pStr, 4, "l");
right = Wrap.NumGet (pStr, 8, "l");
bottom = Wrap.NumGet (pStr, 12, "l");

Do not forget that we have the absolute coordinates of the window (form component and m) below, we can change the window size (at least 3 ways to Win 32 API + FormEx).

A little later, we consider an example of how all the same to process external event if the window does not have focus, or open the drop-down menu on the form (created in different versions of the PM and normal - VybratZnachenie)

1C:Enterprise Developer's Community