Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > VB6 > Вызов API функций


Автор: Гость_Archer 21.12.2005, 07:56
Не погу понять одной тонкости (если она вобще есть).
При вызове API функций для некоторых требуется синтаксис со скобками, для других без скобок:

s4.SetFieldValue_Articles ("Наименование", s_name) - вызывает ошибки
s4.SetFieldValue_Articles "Наименование", s_name - работает без проблем

хотя другие API функции того же объекта работают только со скобками:
designation = s4.GetFieldValue("Обозначение")

В чем тут фишка?

Автор: boevik 21.12.2005, 08:31
Если вызывается процедура (т.е. не определено возвращаемое значение), то вызов обязательно без скобок.
Если вызывается функция и при вызове присваивается возвращаемое значение какой либо переменной, то вызов обязательно со скобками.

Автор: cardinal 21.12.2005, 17:01
Цитата(MSDN)
Calling Procedures
The techniques for calling procedures vary, depending on the type of procedure, where it's located, and how it's used in your application. The following sections describe how to call Sub and Function procedures.

Calling Sub Procedures
A Sub procedure differs from a Function procedure in that a Sub procedure cannot be called by using its name within an expression. A call to a Sub is a stand-alone statement. Also, a Sub does not return a value in its name as does a function. However, like a Function, a Sub can modify the values of any variables passed to it.

There are two ways to call a Sub procedure:

' Both of these statements call a Sub named MyProc.
Call MyProc (FirstArgument, SecondArgument)
MyProc FirstArgument, SecondArgument

Note that when you use the Call syntax, arguments must be enclosed in parentheses. If you omit the Call keyword, you must also omit the parentheses around the arguments.

Calling Function Procedures
Usually, you call a function procedure you've written yourself the same way you call an intrinsic Visual Basic function like Abs; that is, by using its name in an expression:

' All of the following statements would call a function
' named ToDec.
Print 10 * ToDec
X = ToDec
If ToDec = 10 Then Debug.Print "Out of Range"
X = AnotherFunction(10 * ToDec)

It's also possible to call a function just like you would call a Sub procedure. The following statements both call the same function:

Call Year(Now)
Year Now

When you call a function this way, Visual Basic throws away the return value.

Calling Procedures in Other Modules
Public procedures in other modules can be called from anywhere in the project. You might need to specify the module that contains the procedure you're calling. The techniques for doing this vary, depending on whether the procedure is located in a form, class, or standard module.

Procedures in Forms
All calls from outside the form module must point to the form module containing the procedure. If a procedure named SomeSub is in a form module called Form1, then you can call the procedure in Form1 by using this statement:

Call Form1.SomeSub(arguments)

Procedures in Class Modules
Like calling a procedure in a form, calling a procedure in a class module requires that the call to the procedure be qualified with a variable that points to an instance of the class. For example, DemoClass is an instance of a class named Class1:

Dim DemoClass as New Class1
DemoClass.SomeSub

However, unlike a form, the class name cannot be used as the qualifier when referencing an instance of the class. The instance of the class must be first be declared as an object variable (in this case, DemoClass) and referenced by the variable name.

For More Information   You can find details on object variables and class modules in "Programming with Objects."

Procedures in Standard Modules
If a procedure name is unique, you don't need to include the module name in the call. A call from inside or outside the module will refer to that unique procedure. A procedure is unique if it appears only in one place.

If two or more modules contain a procedure with the same name, you may need to qualify it with the module name. A call to a common procedure from the same module runs the procedure in that module. For example, with a procedure named CommonName in Module1 and Module2, a call to CommonName from Module2 will run the CommonName procedure in Module2, not the CommonName procedure in Module1.

A call to a common procedure name from another module must specify the intended module. For example, if you want to call the CommonName procedure in Module2 from Module1, use:

Module2.CommonName(arguments)

Автор: Fighter 22.12.2005, 22:24
Код

Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
Dim a As Integer

Private Sub Command1_Click()
    a = WinExec("calc.exe", 0)
End Sub




Вот тебе кусок кода. Смотри и разбирайся.


Цитата(boevik @ 21.12.2005, 08:31)
Если вызывается процедура (т.е. не определено возвращаемое значение), то вызов обязательно без скобок.
Если вызывается функция и при вызове присваивается возвращаемое значение какой либо переменной, то вызов обязательно со скобками.


А так как WinAPI - это в основном функции, то апишки тебе наверное придется вызывать все время со скобками.

Автор: Exception 22.12.2005, 23:01
Если ты хочешь получить от ф-ции значение, скобки ставятся. Если значение не используется или это -- процедура, скобки опускаются. Вроде все :)

Автор: Fighter 23.12.2005, 01:06
Цитата(Run @ 22.12.2005, 23:01)
Если ты хочешь получить от ф-ции значение, скобки ставятся. Если значение не используется или это -- процедура, скобки опускаются. Вроде все smile


А мне кажется, что если это процедура, то ты МОЖЕШЬ скобки ставить а МОЖЕШЬ НЕ СТАВИТЬ, а если функция, то тогда ты ДОЛЖЕН ставить скобки.

Автор: Exception 23.12.2005, 10:21
Цитата(Fighter @ 23.12.2005, 02:06)

А мне кажется

А ты проверь...
Код

Sub MySub(par As String)
End Sub
Function MyFunc(par As String) As Integer
MyFunc = 0
End Function
Sub Main
MySub "Тест"
Call MySub("Тест")
MyFunc "Тест"
Dim res As Integer
res = MyFunc("Тест")

Странный вопрос како-то smile

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)