Working over a COM connection is easier than you think. Three pillars of working with COM objects

Print (Ctrl+P)

One of the options for exchanging data between 1C databases is exchange via a COM connection. Using a COM connection, you can connect from one 1C database to another and read or write data. This method can be used both in client-server versions of databases and in file databases. This article discusses these types of connections on platform 8.3

com connection

You can create two types of COM objects for the 1C application. These are ole connections V83.Application and com connections V83.COMConnector . In the case of V83.Application An almost full-fledged copy of the 1C application is launched. In case of use V83.COMConnector A small server part is launched. The speed of operation in this case is higher, but some functions may not be available. In particular, working with forms and common modules for which the property of working with external connections is not set. Mostly you should use V83.COMConnector and only in case of lack of functionality V83.Application. The difference in operating speed can be especially noticeable on large-volume databases. For platform 8.2 used V82.Application or V82.COMConnector

Establish an OLE connection

Connection = New COMObject(“V83.Application” ) ;

Establish a COM connection

Connection = New COMObject(“V83.COMConnector” ) ;

Connection string

//For the client-server option
Connection String= “Srvr = ““ServerName” “;Ref = “ “BaseName” ;
//For file mode option:
Connection String= “File = ““PathKBase” “; Usr = UserName; Pwd = Password”;
Attempt
Connection = Connection . Connect(ConnectionString) ;
Exception
Message = New MessageToUser;
Message . Text = “Failed to connect to the database” + DescriptionErrors(); Message . Report();
EndAttempt ;

Disconnection

Connection = Undefined ;
For object V83.Application It is necessary to terminate the connection, otherwise an incomplete session will remain hanging, which will then have to be deleted manually. In the case of V83.COMConnector the connection is broken automatically when the procedure in which the connection was made is completed. And there is one more small point. For the user under whom the connection is being made, the “Request confirmation when closing the program” checkbox must be disabled in its settings.

NewObject() method

To create a new object, you can use the NewObject() method, for example:

For V83.COMConnector

RequestCOM = Connection. NewObject( "Request ") ;
TableCOM = Connection. NewObject( “Table of Values”) ;
ArrayCOM = Connection. NewObject(“Array” ) ;

ViewCOM =Connection.NewObject

For V83.Application

RequestOLE = Connection. NewObject(“ Request ") ;
TableOLE = Connection. NewObject(“Table of Values”) ;
ArrayOLE = Connection.NewObject(“Array”);
ViewCOM =Connection.NewObject(“UniqueIdentifier”, StringUID);

RequestCOM . Text ="CHOOSE
| Positions of Organizations. Code,
| Positions of Organizations.Name
|FROM | Directory.Positions of Organizations
HOW TO POSITIONS OF ORGANIZATIONS”;

Result = RequestCOM. Run();
Sample = Result. Choose () ;
Bye Selection. Next()Cycle
EndCycle ;
You can also use configuration object managers:
DirectoryCOM = Connection. Directories. DirectoryName;
DocumentCOM = Connection. Documents. DocumentName;
RegisterCOM = Connection. Information Registers. RegisterName ;

Receiving and comparing enumeration via COM connection

To compare the values ​​of enumeration elements defined in the configuration, it is necessary to convert these elements to one of the primitive types, the comparison of which is easy. Such types can be either a numeric type or a string type. You can convert the value of an enumeration element to a numeric type like this:

Enum Item = Connection.Directories.Directory1.FindByCode(1).Props1;

PossibleValues ​​= Enum Element.Metadata().Enum Values;

EnumerationElementNumber = PossibleValues.Index(PossibleValues.Find(Connection.XMLString(EnumerationElement)));

If EnumerationItemNumber = 0 Then Report( “Enumer value1”);

ElseIfEnumerationItemNumber = 1 Then Report("EnumerationValue2");

endIf;

Retrieving an object via COM by identifier

Through configuration object managers we get a com object, for example:
DocumentCOM = Connection. Documents. DocumentName;

Then we get a unique identifier string:

StringUID =Connection.string ( DocumentCOM.UniqueIdentifier())

Identifier = New U uniqueIdentifier(StringUID);
WITH linkByIdentifier = Documents[DocumentName].GetLink(Identifier);

If you need to find a com object by document by identifier, then you need to write like this:

WidCOM = Connection.NewObject(“UniqueIdentifier”, StringUID);
LinkByIdentifier = Connection.Documents[DocumentName].GetLink(WidCOM);

One of the options for exchanging data between 1C databases is exchange via a COM connection.

Using a COM connection, you can connect from one 1C database to another and read or write data. This method can be used both in client-server versions of databases and in file databases. In this article we will look at examples of this type of connection. The examples use platform 8.2.

You can create two types of COM objects for the 1C application. This V82.Application And V82.COMConnector. In the case of V82.Application An almost full-fledged copy of the 1C application is launched. in case of use V82.COMConnector A small server part is launched.
The speed of operation in this case is higher, but some functions may not be available. In particular, working with forms and common modules for which the property of working with external connections is not set. Mostly you should use V82.COMConnector and only in case of lack of functionality V82.Application. The difference in operating speed can be especially noticeable on large-volume databases.

So let's get started

  1. Let's create a COM object
    • For V82.Application Connection = New COMObject("V82.Application" ) ;
    • For V82.COMConnector Connection = New COMObject("V82.COMConnector" ) ;
  2. Let's create a connection string
    • for the server version of the database ConnectionString = "Srvr = " "ServerName" ";Ref = " "BaseName" ;
    • for the file version of the database ConnectionString = "File = " "PathKBase" "; Usr = UserName; Pwd = Password";
  3. Connecting to the database Attempt Connection = Connection. Connect(ConnectionString) ; Exception Message = New MessageToUser; Message. Text = + ErrorDescription() ; Message. Report() ; EndAttempt ;
  4. Disconnecting from the database Connection = Undefined ;

    For object V82.Application It is necessary to terminate the connection, otherwise an incomplete session will remain hanging, which will then have to be deleted manually. In the case of V82.COMConnector the connection is broken automatically when the procedure in which the connection was made is completed. And there is one more small point.

    For the user under whom the connection is being made, the “Request confirmation when closing the program” checkbox must be disabled in its settings.

Now let's put all the code together

Connection = New COMObject("V82.Application" ) ; //Connection = New COMObject("V82.COMConnector"); ConnectionString = "Srvr = " "Server1C" ";Ref = " "MyBase" "; Usr = Petya; Pwd = 123" ; //ConnectionString = "File = ""C:\MyBase""; Usr = Petya; Pwd = 123"; Attempt Connection = Connection. Connect(ConnectionString) ; Exception Message = New MessageToUser; Message. Text = "Could not connect to the database"+ DescriptionError() ; Message. Report() ; EndAttempt ; Connection = Undefined ;

For connection type V82.Application the method is used for the COM object that was created initially, and for V82.COMConnector method is applied to the connection. Then the request is processed using standard 1C tools. in code it looks like this:

Request = Connection. NewObject("Request" ) ; // For V82.COMConnector Request = Connection. NewObject("Request" ) ; // For V82.Application Request. Text = "SELECT | Positions of Organizations. Code, | Positions of Organizations.Name|FROM | Directory. Positions of Organizations AS Positions of Organizations"; Result = Request. Run(); Sample = Result. Choose() ; Bye Selection. Next() Loop EndLoop ;

For version 1C:Enterprise 8.3 everything remains unchanged except that when creating COM objects you must use "V83.COMConnector" or "V83.Application".


Keywords: COM, connection, external, OLE, Automation, Connect, ComConnector, Srvr

When using 1C:Enterprise 8.0 COM connections to access data, there are the following advantages compared to using an Automation server:

  1. Faster connection establishment, since there is no need to create a separate operating system process, and all actions are performed within the calling process;

  2. Faster access to the properties and methods of 1C:Enterprise objects, since organizing an access does not require interprocess communication;
  3. Less consumption of operating system resources.

In general, working with 1C:Enterprise 8.0 via a COM connection is similar to working with 1C:Enterprise in server Automation mode. The main differences are as follows:

  1. In the case of an Automation server, a full-fledged 1C:Enterprise 8.0 application is launched, and in the case of a COM connection, a relatively small in-process COM server is launched.

  2. When working via a COM connection, functionality in one way or another related to the organization of the 1C:Enterprise 8.0 user interface is not available;
  3. When operating a COM connection, the 1C:Enterprise 8.0 configuration application module is not used. Its role when working with a COM connection is played by the external connection module.

1.1 Procedure for establishing a COM connection

To organize access to 1C:Enterprise 8.0 data via a COM connection, the following sequence of actions is performed:

  1. a COM object is created with the identifier V8.COMConnector, with the help of which the connection is established;

  2. the Connect method of the previously created V8.COMConnector object is called. The Connect method returns a link to a COM connection object with the 1C:Enterprise 8.0 infobase;
  3. Through the received COM connection object, the valid methods, properties and objects of the infobase with which the connection is established are accessed.

Important! Due to the lack of a user interface in a COM connection, not all objects, properties and methods can be used in a COM connection.

1C:Enterprise objects accessible externally via a COM connection:

  1. Exported variables and procedures/functions of the external join module

  2. Exported variables and procedures/functions of common modules
  3. Including and excluding entire modules by setting properties of common modules

  4. Including and excluding fragments of common modules using a preprocessor
  5. Global context 1C:Enterprise 8.0, with the exception of objects tightly linked to the client application (TextDocument, TabularDocument, ...)

1.2 External connection module

As already noted, the responsibilities of the application module when working through a COM connection are performed by the external connection module. This module may have event handler procedures When SystemStarts() and WhenSystemCompletes(), which can contain actions performed upon initialization and termination of the connection, respectively.

Procedures, functions and global variables defined in an external join module with the Export keyword become, as in the case of an application module, part of the global context.

1.3 Common modules

The properties "Client", "Server" and "External Connection" have been introduced for common modules. They are intended to determine in the configuration the use of modules in the client - server version and in COM connection mode.

1.4 Object "V8.COMConnector"

The only task solved by the V8.COMConnector COM object is establishing a COM connection with the 1C:Enterprise 8.0 information base. An unlimited number of connections can be established using one instance of the V8.COMConnector object. The V8.COMConnector object has a single Connect method, designed to establish a COM connection with the 1C:Enterprise 8.0 infobase.

<СтрокаСоединенияИБ>

The connection line with the information security system is a chain of fragments of the form Parameter=Value. Fragments are separated from each other by ";". If a value contains whitespace, it must be enclosed in double quotes (").

General parameters:

Usr - username;
Pwd - password.

The following parameter is defined for the file version:

File - infobase directory.

The following parameters are defined for the client-server option:

Srvr - 1C:Enterprise server name;
Ref - name of the infobase on the server.

The Connect method establishes a COM connection to the 1C:Enterprise 8.0 infobase and returns a link to the COM connection object.

// A connector object is created
V8 = New COMObject("V8.COMConnector");
// a COM connection object is created
Connection = V8.Connect("File=""c:\InfoBases\Trade""; Usr=""Director"";")

1.5 COM connection object

A COM connection to the 1C:Enterprise infobase provides full access to its global context (see “Program module execution context”). Therefore, a COM connection can have as its methods: system constants, values ​​specified in the configurator of objects that are accessed using managers (for example, constants, enumerations, directories, documents, document logs, reports, processing, plans for types of characteristics, plans accounts, plans of calculation types, registers), as well as variables declared in the external connection module with the Export keyword.

In addition, the COM connection has an additional NewObject method that can be used to create values ​​of certain types.

tk = Connection. NewObject("ValueTable");

String method Allows you to obtain string representations of 1C:Enterprise values.

View = Connection.String(Data.UniqueIdentifier());

1.6. Features of working with a COM connection

In Automation and in a COM connection, TRUE and FALSE have the following values: -1 (minus one) and 0.

It is possible to organize a pool of COM connections. At the same time, several COM connection objects are created IN ADVANCE on the receiving server 1C:Enterprise and it takes even less time to establish a connection, since there is no need to create a new object.

A new Query Builder object has been implemented, designed to generate query texts based on the specified settings. This object supports report builder functionality that is not related to outputting the report to a spreadsheet document or other user interface-related tasks. This object can be used on the 1C:Enterprise server and in a COM connection.

You can use COM objects when running the built-in language on the 1C:Enterprise server.

COM errors are converted to embedded language exceptions.

If the configuration attempts to create an invalid object, such as a spreadsheet document, in an external join module, in a shared module, or in an object module, the COM connection may not be established or may be terminated as an exception.

One way to transfer data from one 1C configuration to another is a software connection using COM. Many companies use several different databases, between which there must be certain connections and dependencies. If it is necessary not only to transfer data, but also to perform certain data processing, then a COM connection will be the optimal mechanism. The ability to analyze data from another 1C database is useful to any developer.

We connect via COM to the 1C database

To implement a COM connection in 1C, a special mechanism called COMConnector is used. This object is installed along with the platform and is used to connect infobases. It should be noted that for versions 8.2 and 8.3 objects with different names are used - “V82.COMConnector” and “V83.COMConnector”, respectively.

Remember that the duration of the COM connection to the database costs a license - do not get carried away with simultaneous execution of several connections. This is especially important for organizations that have a limited number of licenses. This issue can be resolved with the help of routine tasks that are executed when there are no active user connections to the information base.

To be able to connect to another database and request the necessary information, you must know the following data:

  1. What type is it - file or client-server;
  2. Where is it located;
  3. What name and password can you use to log in?
  4. What data are you interested in?

From the first three points, to implement a COM connection, you need to create a string of parameters. Depending on the type of information security, it will differ in appearance. Using the received string, a connection is made, with the help of which you can collect data from another database for analysis and processing using any methods.

Connection ParametersFileIB = "File=""Path_to_database""; Usr=""User_name"";Pwd=""Password"""; Connection ParametersClientServerIB = "Srvr=""Server_Name""; Ref=""Database_Name""; Usr=""User_Name""; Pwd=""Password""";

The connection function is simple and should not raise any questions if all parameters are specified correctly. To speed up debugging and analysis of possible errors, it is better to enclose the connection in the “Try” construct. The function will return a value of type “COM object”, with which you will work to obtain the necessary data.

&OnServer Function ConnectToBase() exportConnectionIB Parameters = "File=""E:\1c database\ERP""; Usr=""Administrator"";Pwd=""1"""; V83COMCon= New COMObject("V83.COMConnector"); Attempt Return V83COMCon.Connect(IB Connection Parameters); Exception Report(ErrorDescription()); Return Undefined; EndAttempt; EndFunction

Through a COM connection you can not only select data, but also add it to the database you are connecting to. Remember that we can transfer 4 primitive data types via a COM object. Other types will have to be specified using the platform's built-in search functions. Please note that global platform functions are also called via a COM connection.

We receive data from the 1C database

After you have received the desired object, you need to read data from another database. To do this, we use a request via a COM connection in 1C 8.3 using the received value of the “COM object” type from the function. It is important to first connect to the database and then execute the request. Execution occurs through the NewObject method, specifying the object type in string form as a parameter - “Request”.

&OnServer Procedure TestCOMOnServer() Connection = ConnectToBase(); If TypeValue(Connection) Type("Undefined") Then RequestBPZO = Connection.NewObject("Request"); RequestBPZO.Text = "SELECT first 15 | DirectoryUser.Name AS Name |FROM | Directory.users AS DirectoryUser"; Select = RequestBPZO.Execute().select(); While Selection.next() loop Report(Selection.Number); EndCycle; endIf; End of Procedure >

For example, to obtain information about users of a certain department, we will set a condition in the request through parameters. One parameter will be of a simple type - a string, and the division will be a link to the directory element "Enterprise Structure". The result of the query is a table with the listed fields of the type that they exist in the database to which the COM connection occurred. If you need to convert them to other types, use the standard platform functions:

  • Line();
  • Number();
  • Date().
RequestBPZO = Connection.NewObject("Request"); RequestBPZO.Text = "SELECT first 15 | DirectoryUser.Name AS Name |FROM | Directory.Users AS DirectoryUser I WHERE | DirectoryUser.Department = &RequiredDepartment | And DirectoryUser.Name like ""%"" + &RequiredName+ ""%""" ; Request BPZO.SetParameter("Required Department", Connection. Directories. Enterprise Structure. Find By Code("00-000023")); RequestBPZO.SetParameter("RequiredName","Ekaterina"); Select = RequestBPZO.Execute().select(); While Selection.next() loop Report(Selection.Name); EndCycle;

If you need to transfer an array to the database for selection based on several parameters, for example, departments, the NewObject command is also used. Similarly, you can pass a list or table of values, filling them with elements of another database through a connection. All existing methods of objects and mechanisms of the platform are available to you for searching.

RequestBPZO = Connection.NewObject.("Request"); RequestBPZO.Text = "SELECT first 15 | DirectoryUser.Name AS Name | FROM | Directory.Users AS DirectoryUser I WHERE | DirectoryUser.Department B (&NecessaryDepartment) | And DirectoryUser.Name like ""%"" + &NecessaryName+ ""%" ""; Array of Departments = Connection.NewObject("Array"); Array of Departments.Add(Connection.Directories.Enterprise Structure.Find By Code("00-000023")); Array of Departments.Add(Connection.Directories.Enterprise Structure.Find By Code("00-000038")); Array of Departments.Add(Connection.Directories.Enterprise Structure.Find By Code("00-000046")); Request BPZO.SetParameter("Required Department", Array of Departments); RequestBPZO.SetParameter("RequiredName","Ekaterina"); Select = RequestBPZO.Execute().select(); While Selection.next() loop Report(Selection.Name); EndCycle;

When transferring documents or directory elements, the question of controlling the transfer of a specific object always arises. With the help of COM connections, such problems can be solved through a unique identifier. You need to find an object in the plug-in database by identifier from the current information security using the “GetLink” function, using the identifier as a string. If one is not found, you can create it using a COM connection.

StrIdent = String(Directories.Users.FindByCode("00-0000313").UniqueIdentifier()); If NOT ValueFilled(Connection.Directories.Users.GetLink(Connection.NewObject("UniqueIdentifier", StrIdent))) then NewUser = Connection.Directories.Users.CreateItem(); NewUser.Name = Directories.Users.FindByCode("00-0000313").Name; NewUser.Individual = Directories.Users.FindByCode("00-0000313").Individual; NewUser.Write(); endIf;

Also, a COM connection has the right to use procedures and functions from common 1C modules with the “External connection” property enabled. In addition to this condition, the called function or procedure must be export and not include interactive actions performed on the server. Otherwise, you will see an error about the operation being invalid.

Compound..; VariableFunction = Connection..; function call>general module name>procedure call>general module name>

The possibilities for external connection with another database in 1C are quite extensive and can allow you to perform many tasks. It is important to be able to correctly evaluate the tools and choose the optimal solution. In most cases, this skill emerges only with experience or by studying examples of the work of experienced specialists.